Thread: Odd template compile error

  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 05:17 AM. Reason: platform notes

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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 >( );
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 03: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, 05:45 PM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM