Results 1 to 8 of 8

Thread: template parameter and conversion operator

  1. #1
    Join Date
    Jan 2006
    Posts
    70
    Thanks
    13
    Thanked 5 Times in 5 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default template parameter and conversion operator

    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'


    Qt Code:
    1. #include <string>
    2. using namespace std;
    3.  
    4. class MyType
    5. {
    6. public:
    7. MyType() {}
    8. ~MyType() {}
    9.  
    10. operator char() const { return 'a'; }
    11. operator int() const { return 1; }
    12. operator double() const { return 2.0; }
    13. operator float() const { return 3.0f; }
    14. };
    15.  
    16. template<typename ValType>
    17. void setType(const MyType& val)
    18. {
    19. ValType type;
    20. type = val.operator ValType();
    21. }
    22.  
    23. int main()
    24. {
    25. MyType a;
    26. setType<double>(a);
    27. return 0;
    28. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by bitChanger; 20th April 2006 at 15:38.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: template parameter and conversion operator

    You could try:
    Qt Code:
    1. template< typename ValType >
    2. void setType( const MyType& val )
    3. {
    4. ValType type;
    5. type = static_cast< ValType >( val );
    6. }
    To copy to clipboard, switch view to plain text mode 
    PS. I use g++ 3.3.6 and both versions compile fine.

  3. #3
    Join Date
    Jan 2006
    Posts
    70
    Thanks
    13
    Thanked 5 Times in 5 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: template parameter and conversion operator

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: template parameter and conversion operator

    Quote Originally Posted by bitChanger
    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:
    Qt Code:
    1. #include <iostream>
    2. #include <string>
    3. using namespace std;
    4.  
    5. class MyType
    6. {
    7. public:
    8. MyType() {}
    9. ~MyType() {}
    10.  
    11. operator char() const { cerr << "char" << endl; return 'a'; }
    12. operator int() const { cerr << "int" << endl; return 1; }
    13. operator double() const { cerr << "double" << endl; return 2.0; }
    14. operator float() const { cerr << "float" << endl; return 3.0f; }
    15. };
    16.  
    17. template<typename ValType>
    18. void setType(const MyType& val)
    19. {
    20. ValType type;
    21. type = static_cast< ValType> ( val );
    22. }
    23.  
    24. int main()
    25. {
    26. MyType a;
    27. setType<double>(a);
    28. return 0;
    29. }
    To copy to clipboard, switch view to plain text mode 
    Output:
    $ ./a.out
    double
    You could implement it in a similar way as QVariant::value() was implemented.

  5. The following user says thank you to jacek for this useful post:

    bitChanger (20th April 2006)

  6. #5
    Join Date
    Jan 2006
    Posts
    70
    Thanks
    13
    Thanked 5 Times in 5 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: template parameter and conversion operator

    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:
    1. #include <string>
    2. #include <complex>
    3. using namespace std;
    4.  
    5. class MyType
    6. {
    7. public:
    8. MyType() {}
    9. ~MyType() {}
    10.  
    11. operator char() const { return 'a'; }
    12. operator int() const { return 1; }
    13. operator double() const { return 2.0; }
    14. operator float() const { return 3.0f; }
    15. operator complex<double>() const { return complex<double>(1.0,1.0); }
    16. };
    17.  
    18. template<typename ValType>
    19. void setType(const MyType& val)
    20. {
    21. ValType type;
    22. type = static_cast<ValType>(val);
    23. }
    24.  
    25. int main()
    26. {
    27. MyType a;
    28. setType<complex<double> >(a);
    29. return 0;
    30. }
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Jan 2006
    Location
    Shanghai, China
    Posts
    52
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: template parameter and conversion operator

    Qt Code:
    1. type = val.operator ValType();
    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:
    1. type = static_cast<ValType>(val);
    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:
    std::complex<double>::complex(const std::complex<double>&)
    and
    std::complex<double>::complex(double, double)
    But the second one has two arguments, how can it beambiguous? I cannot understand why...
    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.

  8. #7
    Join Date
    Jan 2006
    Posts
    70
    Thanks
    13
    Thanked 5 Times in 5 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: template parameter and conversion operator

    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.

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: template parameter and conversion operator

    Then maybe something like this:
    Qt Code:
    1. class MyType
    2. {
    3. public:
    4. MyType() {}
    5. ~MyType() {}
    6.  
    7. template< typename T > T convertTo() const;
    8. };
    9.  
    10. template<>
    11. char MyType::convertTo< char >() const { return 'a'; }
    12.  
    13. template<>
    14. int MyType::convertTo< int >() const { return 1; }
    15.  
    16. ...
    17.  
    18. template<>
    19. complex< double > MyType::convertTo< complex< double > >() const
    20. {
    21. return complex< double >( 1.0, 1.0 );
    22. }
    23.  
    24. template< typename ValType >
    25. void setType( const MyType& val )
    26. {
    27. ValType type;
    28. type = val.template convertTo< ValType >();
    29. }
    To copy to clipboard, switch view to plain text mode 

  10. The following user says thank you to jacek for this useful post:

    bitChanger (21st April 2006)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.