代码之家  ›  专栏  ›  技术社区  ›  Nate

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

  •  9
  • Nate  · 技术社区  · 7 年前

    我在用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;
    }
    

    || 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’
    

    事实上,编译器正试图实例化 conflict 模板,而不是比较 领域

    > . 有更好的办法吗?

    1 回复  |  直到 7 年前
        1
  •  7
  •   Shafik Yaghmour    7 年前

    作为 T.C. pointed out 这是本书的主题 CWG active issue 1835 上面说:

    根据6.4.5[基本查找.classref]第1段,

    在类成员访问表达式(8.2.5[expr.ref])中,如果。或->令牌后面紧跟一个标识符,后跟一个<,必须查找标识符以确定<是 对象表达式。如果找不到标识符,则将其删除 在整个后缀表达式的上下文中查找,并应

    鉴于

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

    因为它是依赖的,因此在类中无法查找end 后缀表达式。此查找查找函数模板,使 这个表达形式不正确。

    解决方法是使用 ()

    return (sp->conflict) < 0;
    

    see it live on godbolt