I'm writing some code which uses SSE/AVX via intrinsics. Therefore, I need arrays that are guaranteed to be aligned. I am attempting to make these via _aligned_malloc with the following code:
template<class T>
std::shared_ptr<T> allocate_aligned( int arrayLength, int alignment )
{
return std::shared_ptr<T>( (T*) _aligned_malloc( sizeof(T) * arrayLength, alignment ), [] (void* data) { _aligned_free( data ); } );
}
My question is, how can I reference the data in my arrays with the usual array index notation? I know unique_ptr has a specialization for arrays that calls delete[] for destruction and allows for array index notation (ie myArray[10]
to access the 11th element of the array). I need to use a shared_ptr however.
This code is giving me problems:
void testFunction( std::shared_ptr<float[]>& input )
{
float testVar = input[5]; // The array has more than 6 elements, this should work
}
Compiler output:
error C2676: binary '[' : 'std::shared_ptr<_Ty>' does not define this operator or a conversion to a type acceptable to the predefined operator
1> with
1> [
1> _Ty=float []
1> ]
Is there a way to do this? I am still pretty new to using smart pointers, so I might be screwing up something simple. Thanks for any help!