The following code will compile with MSVC 7.1 and with g++ 2.95.3
However, I need it to compile with g++ 3.4.4 which it does not.
If i'm reading my books correctly the template parameter has infinite possible types while operator conversions have a finite number of types, therefore this breaks the c++ standard.
Can someone help me find a workaround and/or a g++ flag to allow this to work.
Thanks.
g++ 3.x and up give the following error:
main.cpp: In function `void setType(const MyType&)':
main.cpp:21: error: 'const class MyType' has no member named 'operator ValType'
#include <string>
using namespace std;
class MyType
{
public:
MyType() {}
~MyType() {}
operator char() const { return 'a'; }
operator int() const { return 1; }
operator double() const { return 2.0; }
operator float() const { return 3.0f; }
};
template<typename ValType>
void setType(const MyType& val)
{
ValType type;
type = val.operator ValType();
}
int main()
{
MyType a;
setType<double>(a);
return 0;
}
#include <string>
using namespace std;
class MyType
{
public:
MyType() {}
~MyType() {}
operator char() const { return 'a'; }
operator int() const { return 1; }
operator double() const { return 2.0; }
operator float() const { return 3.0f; }
};
template<typename ValType>
void setType(const MyType& val)
{
ValType type;
type = val.operator ValType();
}
int main()
{
MyType a;
setType<double>(a);
return 0;
}
To copy to clipboard, switch view to plain text mode
Bookmarks