CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Wink compile error (with minimal but complete example)

    Code:
    template <typename T>
    struct trait
    {
        template <typename U>
        static void foo()
        {}
    };
    
    template <typename T>
    class myClass
    {
    public:
    
        void bar()
        {
            trait<T>::foo<bool>(); //error: expected primary-expression before 'bool'
                                   //error: expected ';' before 'bool'
        }
    };
    
    int main()
    {
        myClass<bool> a;
        a.bar();
    }
    Using code::blocks + MinGW 4.5
    Works fine with vs2008.

    I'm at a loss. I expect some obscure C++ rule hiding template foo unless it is qualified in some way, but I just can't seem to figure it out...
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: compile error (with minimal but complete example)

    You should disambiguate with template, e.g.,
    Code:
    trait<T>::template foo<bool>()
    similiar idea as using typename to disambiguate a dependent name.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: compile error (with minimal but complete example)

    Code:
    Thank you for testing your code with Comeau C/C++!
    Tell others about http://www.comeaucomputing.com/tryitout !
    
    Your Comeau C/C++ test results are as follows:
    
    
    Comeau C/C++ 4.3.10.1 (Oct  6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
    Copyright 1988-2008 Comeau Computing.  All rights reserved.
    MODE:strict errors C++ C++0x_extensions
    
    "ComeauTest.c", line 16: error: type name is not allowed
              trait<T>::foo<bool>(); //error: expected primary-expression before 'bool'
                            ^
    
    "ComeauTest.c", line 16: error: expected an expression
              trait<T>::foo<bool>(); //error: expected primary-expression before 'bool'
                                  ^
    
    2 errors detected in the compilation of "ComeauTest.c".
    In strict mode, with -tused, Compile failed
    Comeau has the same error. You need to specify "template".

    Edit: I see that laserlight beat me to it.


    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; September 7th, 2010 at 05:14 AM.

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: compile error (with minimal but complete example)

    Quote Originally Posted by laserlight View Post
    You should disambiguate with template, e.g.,
    Code:
    trait<T>::template foo<bool>()
    similiar idea as using typename to disambiguate a dependent name.
    !!!

    I had tried with "typename" (even though foo isn't a type). I though typename and template were aliases. I was even about to answer that your fix didn't work, not realizing there were 2 different keywords.

    You learn something everyday.

    Thanks a lot.

    Quote Originally Posted by Paul McKenzie View Post
    Code:
    Thank you for testing your code with Comeau C/C++!
    Tell others about http://www.comeaucomputing.com/tryitout !
    
    Your Comeau C/C++ test results are as follows:
    
    
    Comeau C/C++ 4.3.10.1 (Oct  6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
    Copyright 1988-2008 Comeau Computing.  All rights reserved.
    MODE:strict errors C++ C++0x_extensions
    
    "ComeauTest.c", line 16: error: type name is not allowed
              trait<T>::foo<bool>(); //error: expected primary-expression before 'bool'
                            ^
    
    "ComeauTest.c", line 16: error: expected an expression
              trait<T>::foo<bool>(); //error: expected primary-expression before 'bool'
                                  ^
    
    2 errors detected in the compilation of "ComeauTest.c".
    In strict mode, with -tused, Compile failed
    Regards,

    Paul McKenzie
    Thanks for confirming that it is an error (not compiler bug)
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  5. #5
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: compile error (with minimal but complete example)

    I don't know much about how compilers work, but would it make writing compilers much more difficult, or would it make them much slower (because they would have to do more passes) if they were required to get it right without the help of disambiguation keywords like "typename" or "template"?

    Sometimes I feel we have to go through unnecessarily many hoops in C++ to get the compiler to understand what we mean...
    Old Unix programmers never die, they just mv to /dev/null

  6. #6
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: compile error (with minimal but complete example)

    Quote Originally Posted by HighCommander4 View Post
    I don't know much about how compilers work, but would it make writing compilers much more difficult, or would it make them much slower (because they would have to do more passes) if they were required to get it right without the help of disambiguation keywords like "typename" or "template"?

    Sometimes I feel we have to go through unnecessarily many hoops in C++ to get the compiler to understand what we mean...
    It's more a question of standard. If what you write is not legal C++, it is better if your compiler turns you down, then just drudge on with illegal C++ code, or worse, makes a wild guess, that turns out to be wrong.

    Maybe the standard is stupid (maybe), but it is the authority.

    EDIT: While these "hoops" do make the compilers life, they are usually there for very extreme or rare border cases, and justified. It can hurt you:
    http://www.parashift.com/c++-faq-lit...html#faq-35.20

    VStudio accepts it, but as you can see, you might be better of if it doesn't...
    Last edited by monarch_dodra; September 7th, 2010 at 01:51 PM.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  7. #7
    Join Date
    Oct 2008
    Posts
    1,456

    Re: compile error (with minimal but complete example)

    Quote Originally Posted by HighCommander4 View Post
    I don't know much about how compilers work, but would it make writing compilers much more difficult, or would it make them much slower (because they would have to do more passes) if they were required to get it right without the help of disambiguation keywords like "typename" or "template"?

    Sometimes I feel we have to go through unnecessarily many hoops in C++ to get the compiler to understand what we mean...
    those disambiguations are necessary to let the compiler diagnose errors as soon as possible, in particular, before template instantion; in order to do so, the compiler must know which expressions are or not type names ( for example, in "p*x" are we declaring a pointer or invoking a multiplication ? is "p<T>x" an ill formed expression involving relational operator or is it a template instantion ? ). So, in theory, they are also necessary for fixing the semantics of templates.

    That said, they are definitely too verbose and their error-preventing abilities might be disputable after all ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured