Consider following class of custom array
template <typename element, unsigned int size>
class array
{
private:
element (&data)[size];
public:
array(element (&reference)[size]):data(reference){}
array():data(new element [size]){} // This is where my problem arises
};
Obviously, in initialization list of constructor evaluates the rvalue expression (the new-expression) to rvalue of pointer instead of reference type. So in MSVC (Microsoft Visual Studio 2012 RC) prompts the error about type conversion from element * to element (&)[size].
However, I think that if I can perhaps convert the rvalue from pointer to reference, it should compile. Is there any chance I can make it there in initialization list of constructor? Or perhaps it just all conflicts with the principle of C++ language? I’ve investiaged into reinterpret_cast, but the only rule I find relevant is
“An lvalue expression of type T1 can be converted to reference to
another type T2. The result is an lvalue or xvalue referring to the
same object as the original lvalue, but with a different type.” – cppreference.com
which clearly states that the conversion applies to lvalue expression.
OK, I know what you might think now – why do you need such class of custom array and why don’t you just composite pointer instead of the problemtic reference? Well…you always need different approaches to choose before knowning the best don’t you?
What’s this about C++11 and rvalue references? To get a lvalue expression to the value pointed to by a pointer, just use the unary
*operator. It’s been part of the language since the beginning. All you have to do here is make sure you get a pointer to an array, rather than a pointer to the first element of the array.As mentioned by Mat in his comment, the parameterless constructor here is a very bad idea, because the memory will not be freed. Hopefully, having a working code sample will allow you to experiment a bit and come to the same conclusion.