开发者

GCC error when compiling MS VC++ code with templates

we're taking some code written for Visual Studio 2008 and try to compile it with gcc. We experienced an error in the following code (simplified to what's necessary):

template<int R, int C, typename T>
struct Vector
{
 template <typename TRes>
 TRes magnitude() const
 {
  return 0;
 }

};

struct A
{
 typedef Vector<3,1,int> NodeVector;
};

template<class T>
struct B
{
 void foo()
 {
  typename T::NodeVector x;
  x.magnitude<double>()开发者_JAVA百科; //< error here
 }
};

...
    B<A> test;
    test.foo();

GCC says

error: expected primary-expression before 'double'
error: expected `;' before 'double'

Can you explain the error to me? What's a cross-compiler solution?

Thanks a lot!


The problem is that since the C++ compiler doesn’t know the actual type of T (let alone T::NodeVector it doesn’t know that magnitude is supposed to be a template. You need to specify that explicitly:

x.template magnitude<double>();

Otherwise C++ will parse the tokens as x, operator., magnitude, operator<, double, operator>

The GCC is right, by the way. MSVC++ is notoriously lax on such matters.


At the point of B it has no way to know what type x is, and that magnitude will be a template function so you need to declare it as one first.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜