此代码无法在g ++(Ubuntu/Linaro 4.6.3-1ubuntu5)4.6.3上编译,出现此错误
test.cpp: In function ‘T mul(V&, V&)’:
test.cpp:38:27: error: expected primary-expression before ‘>’ token
test.cpp:38:29: error: expected primary-expression before ‘)’ token
test.cpp:38:53: error: expected primary-expression before ‘>’ token
test.cpp:38:55: error: expected primary-expression before ‘)’ token
Run Code Online (Sandbox Code Playgroud)
但它在Microsoft C/C++优化编译器版本15.00.21022.08 for x64上正确编译和执行
#include <iostream>
#include <complex>
template <class T>
class SM
{
public:
T value;
};
template <class T>
class SC : public SM<T>
{
};
class PSSM {
public:
template <class T>
T & getSC() { return sc; }
private:
SC<double> sc;
};
class USSM {
public:
template <class T>
T & getSC() { return sc; }
private:
SC<std::complex<double> > sc;
};
template <class T, class V>
T mul( V & G, V & S) {
return (G.getSC<SC<T> >().value * S.getSC<SC<T> >().value); // error is here
}
int main() {
PSSM p;
PSSM q;
p.getSC<SC<double> >().value = 5;
q.getSC<SC<double> >().value = 3;
std::cout << mul<double>(p,q);
}
Run Code Online (Sandbox Code Playgroud)
我不明白问题出在哪里.谁能理解如何解决它,或解释g ++中问题的本质?
And*_*owl 48
问题是语法问题.template在这种情况下,您应该使用消歧器,以便正确解析您对成员函数模板的调用:
return (G.template getSC<SC<T> >().value * S.template getSC<SC<T> >().value);
// ^^^^^^^^^ ^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)
这种消除歧义有助于编译器识别后面的内容G.是成员模板特化,而不是例如一个名为getSC后跟a <(小于)的数据成员.
template消除歧义的标准参考是C++ 11标准的第14.2/4段:
当成员模板特化的名称出现在postfix-expression 之后
.或之后,或者出现在qualified-id中->的嵌套名称说明符之后,并且postfix-expression的对象表达式是类型相关的或嵌套名称说明符在qualified-id中引用依赖类型,但名称不是当前实例化的成员(14.6.2.1),成员模板名称必须以关键字为前缀.否则,假定该名称命名非模板.[ 例如:templateRun Code Online (Sandbox Code Playgroud)struct X { template<std::size_t> X* alloc(); template<std::size_t> static X* adjust(); }; template<class T> void f(T* p) { T* p1 = p->alloc<200>(); // ill-formed: < means less than T* p2 = p->template alloc<200>(); // OK: < starts template argument list T::adjust<100>(); // ill-formed: < means less than T::template adjust<100>(); // OK: < starts template argument list }- 结束例子 ]
| 归档时间: |
|
| 查看次数: |
4297 次 |
| 最近记录: |