So I'm implementing a native arrays wrapper which will allow such to be passed as function arguments and to be returned. I'm having a trouble however with casting it to a native array as native arrays can't be returned. As an replacement I decided to use 'rvalue' reference return type of the casting operator but this will not act correctly because if I want to bind the returned object into an 'rvalue' reference in order to extend it's life-time this won't happen as it's an 'xvalue' and not 'prvalue'. Is there any solution for the problem? Maybe some 'prvalue' cast? Or if there is some other way to implement this implicit cast to 'array'?
The class:
template<typename type>
struct tmp
{
tmp() {}
tmp(const tmp &) = default;
tmp(const type & arg) : tmp(*(const tmp*)arg) {}
&& operator type() && {return static_cast<type&&>(d);}
~tmp () { cout << "tmp destructor" << endl; }
type d;
};
And the code that uses it:
tmp<tmp<int [4]>> Func() // used 'tmp<int [4]>' instead of array to track object destruction (but normally it should be an native array type
{
return tmp<tmp<int [4]>>();
}
int main()
{
tmp<int [4]> &&tmp1 = Func(); //implicit cast (from 'tmp<tmp<int [4]>>') to 'tmp<int [4]>', calls tmp::operator type()
cout << "Here" << endl;
return 0;
}
The program output:
tmp destructor
tmp destructor
Here
As you see the return value of the cast operator is not extended.
Life example.