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?
new
to fit your use case. How would you ever be able to delete that correctly? – Mat