[begin main.cpp]
template<typename ClassType>
struct S
{
template<typename FunctionType> void member() {};
};
template<typename Type>
void g()
{
S<Type> s;
/*line 11*/ s.member<Type>();
}
void h()
{
S<int> s;
s.member<int>();
}
int main(int, char**)
{
g<int>();
h();
return 0;
}
[end main.cpp]
I compile it with g++ (GCC) 3.4.2 (mingw-special) and get this compiler
error:
../main.cpp: In function `void g()':
../main.cpp:11: error: expected primary-expression before '>' token
../main.cpp:11: error: expected primary-expression before ')' token
I do not understand what is wrong with the expression in Line 11 and
where the difference between the calls in g and h is.
Can anyone explain this?
TIA
Gabriel
Thank you, Frank
Yes, I think that they both try to conform to the standard. And I think
g++ 3.4 (or later) is very close to the standard. As far as I understand
the language, my code should be correct, but I am not sure if the g++
should translate it or if the MSVC makes a mistake by translating it.
If it's a problem with the g++, does anyone know a workaround?
I just ran it under the IAR Embedded Workbench IDE with the MSP430 IAR
C/C++ Compiler, and it tells me
Error[Pe254]: type name is not allowed H:\Projects\Test (IAR Embedded
Workbench IDE)\main.cpp 11
Error[Pe029]: expected an expression H:\Projects\Test (IAR Embedded
Workbench IDE)\main.cpp 11
Gabriel
FWIW, The code that you posted doesn't compile with g++ 3.3.4 as well.
Anyway, as a work around you can add a dummy parameter to the
templated member function.
template<typename ClassType>
struct S
{
template<typename FunctionType> void member(FunctionType* t =0) {};
};
template<typename Type>
void g()
{
S<Type> s;
s.member( static_cast<Type*>(0) );
}
Rgds,
anna
Drop the trailing semicolon: while it is not a severe violation,
it is entirely superfluous and a bad habit.
> };
>
> template<typename Type>
> void g()
> {
> S<Type> s;
> /*line 11*/ s.member<Type>();
Needs to be
s.template member<Type>();
to be standard C++. MS compiler hasn't got there yet.
> }
>
> void h()
> {
> S<int> s;
> s.member<int>();
> }
>
> int main(int, char**)
> {
> g<int>();
> h();
> return 0;
> }
> [end main.cpp]
>
> I compile it with g++ (GCC) 3.4.2 (mingw-special) and get this compiler
> error:
> ../main.cpp: In function `void g()':
> ../main.cpp:11: error: expected primary-expression before '>' token
> ../main.cpp:11: error: expected primary-expression before ')' token
>
> I do not understand what is wrong with the expression in Line 11 and
> where the difference between the calls in g and h is.
>
> Can anyone explain this?
A template member of a template needs to be specified as such to help
the compiler resolve the name. I don't remember the exact paragraph
of the Standard, but you could search the archives on Google, it has
come up more than once in this newsgroup.
V
That's a nifty solution! I think that will help me out for now...
Thanx anna!
Gabriel
Thank you, Victor.
I have never seen this before. Even in the Stroustrup I could not find it.
What I still not understand is why the call in h is valid and the call
in (the template function) g is not valid.
Gabriel
'h' is not a template itself. 'g' is a template. The names are resolved
differently inside those scopes. Get a copy of "C++ Templates" by David
Vandevoorde and Nicolai Josuttis. It's an extremely helpful book. It
doesn't just tell you how, it explains why certain things are done the way
they are done.
V
> Needs to be
> s.template member<Type>();
>
> to be standard C++. MS compiler hasn't got there yet.
I haven't yet written (or maintained) template heavy C++ code.
So, this is new for me (obvious, if you had a look at my
previous post, which the OP was kind enough to call nifty.)
My question is if the original code was incorrect, and MS
compiler compiled it successfully, what does it mean? Will
the MS compiler generate wrong object code for correct
(standard compliant) C++ code, in some very rare cases?
(Probably template heavy stuff.)
Any thoughts on that?
Rgds,
anna
The MS compiler is known to be unable to translate some template heavy code.
Gabriel
It's called a "language extension". See more musings below.
> Will
> the MS compiler generate wrong object code for correct
> (standard compliant) C++ code, in some very rare cases?
> (Probably template heavy stuff.)
>
> Any thoughts on that?
Well, we cannot say anything about the correctness of the generated code,
since it's generated from a non-standard extension. However, the VC++ is
known to also accept the standard code , and all we can do about the
situation is to compare the codes generated from either C++ program (with
and without the 'template' keyword) and conclude that the non-standard
source yields the same machine code, for example.
The behaviour of the two resulting programs (from the non-standard source
and the amended one) can also be compared. Whether it has any bearing on
the results of compiling _similar_ programs, I don't know. Maybe. You
should also ask in microsoft.public.vc.language newsgroup, it's full of
very knowledgeable folk, especially when it comes to VC++.
VC++ has got much better over the past couple of years (I consider v7.1
the first really worth using if you want to learn standard C++, v8 is even
better). But it still has some quirks, especially when it comes to being
able to compile non-standard code without even a warning. AFAIK, VC++
team is not going to do much about those "bugs" in their compiler simply
because VC++ successfully compiles the well-formed code. Issuing warnings
or other diagnostics about ill-formed programs is often up to the
implementors. The compiler is free to keep compiling ill-formed code and
generate what it considers to be the best approximation of what the
programmer wanted the code to do. It's non-portable, yes, but it's fully
within the language specifications.
V
they're called dependent names, and here's a good site which explains
them. http://womble.decadentplace.org.uk/c++/template-faq.html
> /*line 11*/ s.member<Type>();
s.template member<Type>();
's' depends on the template argument, so you need to tell
the compiler that you mean to call a template member
function, and not
operator<( s.member, Type )...
or something like that.
Marc