[英]expected primary-expression before “>” in g++ but not in microsoft compiler
This code fails to compile on g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3, with this error 此代码无法在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
but it compiles and executes correctly on Microsoft C/C++ Optimizing Compiler Version 15.00.21022.08 for x64 但它在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);
}
I don't understand where the problem is. 我不明白问题出在哪里。 Can anyone understand how to work around it, or explain the nature of the problem in g++?
谁能理解如何解决它,或解释g ++中问题的本质?
The problem is syntactic. 问题是语法问题。 You should use the
template
disambiguator in this case, so that your invocation of a member function template will be correctly parsed: 在这种情况下,您应该使用
template
消歧器,以便正确解析您对成员函数模板的调用:
return (G.template getSC<SC<T> >().value * S.template getSC<SC<T> >().value);
// ^^^^^^^^^ ^^^^^^^^^
This disambiguator helps the compiler recognizing that what follows G.
is a member template specialization and not, for instance, a data member called getSC
followed by a <
(less than). 这个消歧器帮助编译器识别
G.
是成员模板特化,而不是例如名为getSC
的数据成员后跟<
(小于)。
The Standard reference for the template
disambiguator is Paragraph 14.2/4 of the C++11 Standard: template
消除器的标准参考是C ++ 11标准的第14.2 / 4段:
When the name of a member template specialization appears after
.
当成员模板专业化的名称出现之后
.
or->
in a postfix-expression or after a nested-name-specifier in a qualified-id , and the object expression of the postfix-expression is type-dependent or the nested-name-specifier in the qualified-id refers to a dependent type, but the name is not a member of the current instantiation (14.6.2.1), the member template name must be prefixed by the keywordtemplate
.或者
->
在postfix-expression中或者在qualified-id中的nested-name-specifier之后, postfix-expression的对象表达式依赖于类型,或者qualified-id中的nested-name-specifier指的是依赖类型,但名称不是当前实例化的成员 (14.6.2.1), 成员模板名称必须以关键字template
为前缀。 Otherwise the name is assumed to name a non-template.否则,假定该名称命名非模板。 [ Example:
[ 例如:
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 }
— end example ]
- 结束例子 ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.