简体   繁体   English

这个模板解析冲突叫什么?

[英]What is this template parsing conflict called?

I am getting an issue compiling this minimal example with g++ 7.3 我正在用g ++ 7.3编译这个最小的例子

template<typename T>
struct conflict
{
};

template<typename T>
struct s
{
    int conflict;
};

template<typename T>
bool go()
{
    s<T>* sp;
    return sp->conflict < 0;
}

The actual error message is less than revealing: 实际的错误消息不足以揭示:

|| test.cpp: In function ‘bool go()’:
test.cpp|16 col 24| error: type/value mismatch at argument 1 in template parameter list for ‘template<class T> struct conflict’
||   return sp->conflict < 0;
||                         ^
test.cpp|16 col 24| note:   expected a type, got ‘0’

In truth the compiler is trying to instantiate the conflict template instead of comparing the conflict field. 实际上,编译器正在尝试实例化conflict模板,而不是比较conflict字段。

Does this error have a name? 这个错误有名字吗?

Also, I fixed it by swapping the comparison to use > . 另外,我通过交换比较来修复它使用> Is there a better way? 有没有更好的办法?

As TC pointed out this is the subject of CWG active issue 1835 which says: 正如TC所指出的,这是CWG活跃问题1835的主题,其中说:

According to 6.4.5 [basic.lookup.classref] paragraph 1, 根据6.4.5 [basic.lookup.classref]第1段,

In a class member access expression (8.2.5 [expr.ref]), if the . 在类成员访问表达式(8.2.5 [expr.ref])中,如果是。 or -> token is immediately followed by an identifier followed by a <, the identifier must be looked up to determine whether the < is the beginning of a template argument list (17.2 [temp.names]) or a less-than operator. 或 - > token后面紧跟一个标识符后跟一个<,必须查找标识符以确定<是模板参数列表的开头(17.2 [temp.names])还是小于运算符。 The identifier is first looked up in the class of the object expression. 首先在对象表达式的类中查找标识符。 If the identifier is not found, it is then looked up in the context of the entire postfix-expression and shall name a class template. 如果未找到标识符,则在整个postfix-expression的上下文中查找它,并命名一个类模板。

Given 特定

  template<typename T> T end(T); template<typename T> bool Foo(T it) { return it->end < it->end; } 

since it is dependent and thus end cannot be looked up in the class of the object expression, it is looked up in the context of the postfix-expression. 因为它是依赖的,因此无法在对象表达式的类中查找end,所以在postfix-expression的上下文中查找它。 This lookup finds the function template, making the expression ill-formed. 此查找查找函数模板,使表达式格式错误。

One possibility might be to limit the lookup to the class of the object expression when the object expression is dependent. 一种可能性是在对象表达式依赖时将查找限制为对象表达式的类。

A fix is to use () : 修复是使用()

return (sp->conflict) < 0;

see it live on godbolt 看到它生活在godbolt上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM