Consider the following program:
#include <iostream>
template<int s>
class Pack
{
public:
Pack(){}
char data[s];
template<typename X> operator X&(){ return *reinterpret_cast<X*>(data); }
template<typename X> operator X const&()const{ return *reinterpret_cast<const X*>(data); }
};
int main()
{
const Pack<8> p;
const double d(p);
std::cout<<d<<std::endl;
}
It compiles under Windows. Under linux, I get:
test.cc: In function ‘int main()’:
test.cc:17: error: passing ‘const Pack<8>’ as ‘this’ argument of ‘Pack<s>::operator X&() [with X = double, int s = 8]’ discards qualifiers
Why? Why doesn't it accept a conversion operator of type const? How can I fix this and still have a convenient template type conversion operator (in the version of const, not const). Thank!
source
share