Results 1 to 8 of 8

Thread: template parameter and conversion operator

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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.