|
-
September 7th, 2010, 04:59 AM
#1
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.
-
September 7th, 2010, 05:03 AM
#2
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.
-
September 7th, 2010, 05:04 AM
#3
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.
-
September 7th, 2010, 05:18 AM
#4
Re: compile error (with minimal but complete example)
 Originally Posted by laserlight
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.
 Originally Posted by Paul McKenzie
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.
-
September 7th, 2010, 11:13 AM
#5
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
-
September 7th, 2010, 01:25 PM
#6
Re: compile error (with minimal but complete example)
 Originally Posted by HighCommander4
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.
-
September 8th, 2010, 02:08 AM
#7
Re: compile error (with minimal but complete example)
 Originally Posted by HighCommander4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|