You could try:PS. I use g++ 3.3.6 and both versions compile fine.Qt Code:
template< typename ValType > void setType( const MyType& val ) { ValType type; type = static_cast< ValType >( val ); }To copy to clipboard, switch view to plain text mode
You could try:PS. I use g++ 3.3.6 and both versions compile fine.Qt Code:
template< typename ValType > void setType( const MyType& val ) { ValType type; type = static_cast< ValType >( val ); }To copy to clipboard, switch view to plain text mode
I've tried to compile it with 3.4.4 and 4.1.0 and got the same error.
In my accual application I can't just do a static cast on val because the conversion operator actually converts a private void* of data into the appropriate type not the class itself.
It does exactly the same thing:Originally Posted by bitChanger
Output:Qt Code:
#include <iostream> #include <string> using namespace std; class MyType { public: MyType() {} ~MyType() {} operator char() const { cerr << "char" << endl; return 'a'; } operator int() const { cerr << "int" << endl; return 1; } operator double() const { cerr << "double" << endl; return 2.0; } operator float() const { cerr << "float" << endl; return 3.0f; } }; template<typename ValType> void setType(const MyType& val) { ValType type; type = static_cast< ValType> ( val ); } int main() { MyType a; setType<double>(a); return 0; }To copy to clipboard, switch view to plain text mode
You could implement it in a similar way as QVariant::value() was implemented.$ ./a.out
double
bitChanger (20th April 2006)
Now I know why I was trying to explicitly call the conversion operator. In my application the type is complex. This gets ambiguous with the complex copy constructor.
Any thoughts on this one?![]()
Qt Code:
#include <string> #include <complex> 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; } operator complex<double>() const { return complex<double>(1.0,1.0); } }; template<typename ValType> void setType(const MyType& val) { ValType type; type = static_cast<ValType>(val); } int main() { MyType a; setType<complex<double> >(a); return 0; }To copy to clipboard, switch view to plain text mode
Codes above does not work coz `ValType` here is a qulified name, so it's looked up only in the scope of val, i.e. MyType, so the `typename ValType` declared in the template function won't be looked up. But I haven't seen any solutions by now...Qt Code:
type = val.operator ValType();To copy to clipboard, switch view to plain text mode
However, it's weired that gcc3.4.2 reports an ambiguous. Gcc says there are two candidates:Qt Code:
type = static_cast<ValType>(val);To copy to clipboard, switch view to plain text mode
But the second one has two arguments, how can it beambiguous? I cannot understand why...std::complex<double>::complex(const std::complex<double>&)
and
std::complex<double>::complex(double, double)
1. Users don't have the manual, and if they did, they wouldn't read it.
2. In fact, users can't read anything, and if they could, they wouldn't want to.
If you look at the 2 candidates that it provides you'll notice that they are constructors for the std::complex class. The compiler is confused and don't even see the conversion operator in this case.
Then maybe something like this:Qt Code:
class MyType { public: MyType() {} ~MyType() {} template< typename T > T convertTo() const; }; template<> char MyType::convertTo< char >() const { return 'a'; } template<> int MyType::convertTo< int >() const { return 1; } ... template<> complex< double > MyType::convertTo< complex< double > >() const { return complex< double >( 1.0, 1.0 ); } template< typename ValType > void setType( const MyType& val ) { ValType type; type = val.template convertTo< ValType >(); }To copy to clipboard, switch view to plain text mode
bitChanger (21st April 2006)
Bookmarks