posted Sat 07-02-2009 17:06:47, in the c++ ( ) category
I thought I had seen it all. Syntactical weirdness in C++ like templated friend definitions, template template parameters, method pointer definitions, but no... there was one more and it dazzled me all day.
I had the following code (simplified for the example), and it worked just fine in Visual Studio 2008, but it barfed in gcc:
template<typename _S, typename _T>
struct foo {
static _S& bar( _S& s, const _T& t ) {
t.test<_S>( s );
return s;
}
};
The error gcc spewed was:
a.cpp: In static member function 'static _S& foo<_S, _T>::bar(_S&, const _T&)':
a.cpp:4: error: expected primary-expression before '>' token
But why? It shouldn't be complaining about the templated method call on t, as it has no idea what t is, right? It complains during template compilation, not during instantiation (as there is none in that snippet of code!) Visual Studio didn't complain, and for once I thought it might be right....
But ofcourse it wasn't ;) There is actually a way to satisfy a compiler like gcc in this particular scenario. For you C++ coders out there, do you know what it is? :-)
I'll put the answer in here but invisible. To see it, select the black box below.
template<typename _S, typename _T>
struct foo {
static _S& bar( _S& s, const _T& t ) {
t.template test<_S>( s );
return s;
}
};
-- Foddex
[ Back to blog listing ]
| More c++...20102009200820062005 |