Odd template compile error

This is a discussion on Odd template compile error within the C++ Programming forums, part of the General Programming Boards category; Here's a stripped-down example of the problem: Code: template < typename Type > struct foo { template < typename Other ...

  1. #1
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    Odd template compile error

    Here's a stripped-down example of the problem:

    Code:
    
    template < typename Type >
    struct foo
    {
        template < typename Other >
        Other bar( void ) const
        {    
            return Other( );
        }
    };
    
    template < typename Type >
    void baz( foo< Type > const& object )
    {
        object.bar< int >( );
    }
    
    int main( void )
    {
        baz( foo< double >( ) );    
    }
    The compiler complains: "expected primary-expression before 'int'" and "expected ';' before 'int'".

    Incidentally, with a typedef the problem goes away:

    Code:
    
    template < typename Type >
    struct foo
    {
        template < typename Other >
        Other bar( void ) const
        {    
            return Other( );
        }
    };
    
    typedef foo< double >
        foo_double;
    
    void baz( foo_double const& object )
    {
        object.bar< int >( );
    }
    
    int main( void )
    {
        baz( foo_double( ) );    
    }
    Unfortunately, I can't use the typedef approach here - I just thought I'd point it out.

    Any ideas?

    EDIT:

    I'm using gcc 4.4.0 mingw.
    Last edited by Sebastiani; 12-01-2009 at 04:17 AM. Reason: platform notes

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,931
    My instincts tell me that you need to disambiguate a template:
    Code:
    template < typename Type >
    void baz( foo< Type > const& object )
    {
        object.template bar< int >( );
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Your instincts were correct! I still don't understand why the disambiguation is necessary, though. Do you know of any good links on the issue?

    Anyway, thanks so much, Laserlight. You really are a C++ witch!

    Cheers.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,931
    Quote Originally Posted by Sebastiani
    Your instincts were correct!
    Yeah, I checked it myself just before posting in case they were wrong, heheh.

    Quote Originally Posted by Sebastiani
    I still don't understand why the disambiguation is necessary, though. Do you know of any good links on the issue?
    I read of this in Stephen Dewhurst's book C++ Common Knowledge.

    Quote Originally Posted by Sebastiani
    You really are a C++ witch!
    But yeah, it seems more like witchcraft than common knowledge
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by laserlight
    I read of this in Stephen Dewhurst's book C++ Common Knowledge.
    Looks like a really good book, actually. Lot's of positive reviews. It's fairly priced, too. <heading over to amazon.com>

    Quote Originally Posted by laserlight
    But yeah, it seems more like witchcraft than common knowledge.
    No doubt!

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    template < typename Type >
    void baz( foo< Type > const& object )
    {
        object.bar< int >( );
    }
    I may be mistaken, but the compiler basically shouldn't know about template methods within another template class, so it has to interpret < as less-than. (When first pass is done to check non-dependent stuff, which has to be correct by the standard regardless of any instantiations.)

    It should be similar to using typename in front of type names nested in template classes: the compiler has to assume it is a non-type identifier otherwise.

    Again, technically it is possible that there is a specialization of foo where bar is not a templated member function. (The point being, probably, not to allow situations where the same code would indeed result in different meaning, given different specializations.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking to shlwapi.lib in C, MSVC CMD.
    By Joerge in forum Windows Programming
    Replies: 4
    Last Post: 08-07-2009, 05:18 PM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  3. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21