:::: www.foddex.net ::::
      Home      |      Blog      Tutorials      Reviews      |      Resume      Portfolio      |      Photos      Stuff      |      Unreal      Flame      |      Links
Foddex' head
Contact me

Stuff
NL Weather
NL Precipitation
Temperature
xkcd
ICHC
Slashdot
Kitten
wliia
TBBT - Zoom Zoom!!

BLOG: C++ syntactical weirdness

posted Sat 07-02-2009 17:06:47, in the c++ (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 ]

0 comment(s)

Leave a comment

Name (required)
URL
Copy this code0-324-293-051

Pressure building on my soul
I ask God to take control
Guide me through this fucked up world

-

I know someday you'll have a beautiful life, I know you'll be a star
In somebody else's sky, but why, why, why
Can't it be, can't it be mine