Constant constant type conversion operator, not working under linux (gcc)

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>’ asthis’ 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!

+5
source share
1 answer

In accordance with the C ++ 03 standard, this code is poorly formed, because the output of the template argument cannot be deduced X&or X const&against const double.

++ 03 , , , . ++ 0x , , ,

: GCC ​​ double ( cv- !) X X const. X , const Pack<8> - GCC const . ,

// can't strip cv-qualifiers off "double const&" - there are no top-level 
// cv qualifiers present here!
double const &d(p);
+5

All Articles