Results 1 to 5 of 5

Thread: gcc problem: c++ tmplates

  1. #1
    Join Date
    Sep 2006
    Posts
    27
    Thanks
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default gcc problem: c++ tmplates

    Hi All,

    I'm currently using gcc 4.20 and I'm running into problems with templates. Afaik the latest gcc versions are quit strict in meeting the standard, so I'm probably doing something wrong, but I don't see what.. (reading the standard didn't help, the lingo is quite weird ;-)

    here's a simplified test case:
    Qt Code:
    1. template< class T >
    2. struct tTest
    3. {
    4. template< class T2 >
    5. static T2 func()( const T& ac_tType )
    6. {
    7. //actual code does stuff to convert T->T2 here
    8. return T2();
    9. }
    10. };
    11.  
    12. template< class T >
    13. struct tUsesTest
    14. {
    15. typedef tTest< T > mt_Test;
    16.  
    17. void func()
    18. {
    19. T test;
    20. int nConverted = mt_Test::func( test );
    21. }
    22. };
    23.  
    24.  
    25. int main( argc, argv )
    26. {
    27. tUsesTest< unsigned > test;
    28. test.func();
    29. return 0;
    30. }
    To copy to clipboard, switch view to plain text mode 

    this gives:
    20: error: no matching function for call to 'tTest<unsigned int>::func(unsigned int&)'

    replacing line 20 with
    Qt Code:
    1. int nConverted = mt_test::func< int >( test );
    To copy to clipboard, switch view to plain text mode 

    yields:
    20: error: expected primary-expression before 'int'

    Can anyone explain in understandable language why this fails, and how to code this correctly?
    Thanks in advance!

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: gcc problem: c++ tmplates

    Qt Code:
    1. template< class T >
    2. struct tTest
    3. {
    4. template< class T2 >
    5. static T2 func( const T& ac_tType )
    6. {
    7. //actual code does stuff to convert T->T2 here
    8. return T2();
    9. }
    10. };
    11.  
    12. template< class T >
    13. struct tUsesTest
    14. {
    15. typedef tTest< T > mt_Test;
    16.  
    17. void func()
    18. {
    19. T test;
    20. int nConverted = mt_Test::func<unsigned int>( test );
    21. }
    22. };
    23.  
    24. int main(int argc, char* argv[])
    25. {
    26. tUsesTest< int > test;
    27. test.func();
    28. return 0;
    29. }
    To copy to clipboard, switch view to plain text mode 

    You had a couple of errors back there. For example you did not specify the T2 type for func in tUsesTest. So the compiler could not deduce the type out of thin air
    .
    And in tTest, func had a weird syntax. It did resembled a bit with pointer to function syntax, but it wasn't correct.

    Try the code I posted. Should work with gcc too.

  3. #3
    Join Date
    Sep 2006
    Posts
    27
    Thanks
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: gcc problem: c++ tmplates

    Quote Originally Posted by marcel View Post
    You had a couple of errors back there. For example you did not specify the T2 type for func in tUsesTest. So the compiler could not deduce the type out of thin air
    .
    And in tTest, func had a weird syntax. It did resembled a bit with pointer to function syntax, but it wasn't correct.
    ok sorry for the typos..

    Try the code I posted. Should work with gcc too.
    well that's the same as what I did here
    Qt Code:
    1. int nConverted = mt_test::func< int >( test );
    To copy to clipboard, switch view to plain text mode 

    and that doesn't work with gcc (tried both 4.0.1 and 4.2).. it does with msvc8.0, but I'm not sure which of the two is correct here.

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: gcc problem: c++ tmplates

    Try this:

    Qt Code:
    1. int nConverted = mt_Test::template func<unsigned int>( test );
    To copy to clipboard, switch view to plain text mode 
    You got your error because func is dependent on the template type, which is resolved at instantiation time( your function is static.

    BTW, you can use ->template when accessing template functions for pointers and .template for objects.

    These cool things have been introduced in the standard by necessity( my opinion ).

    regards
    Last edited by marcel; 31st August 2007 at 20:07.

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

    stinos (1st September 2007)

  6. #5
    Join Date
    Sep 2006
    Posts
    27
    Thanks
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: gcc problem: c++ tmplates

    well thanks alot! that does indeed do the trick!

Similar Threads

  1. Replies: 16
    Last Post: 7th March 2006, 15:57
  2. general sqlplugin problem
    By a550ee in forum Installation and Deployment
    Replies: 5
    Last Post: 28th February 2006, 10:03

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
  •  
Qt is a trademark of The Qt Company.