c++ - 非模板函数接受模板消歧器

标签 c++ templates

这段代码

template <typename U>
struct Bar{
    void setNumber(int) {}
};

template <int N>
struct Asd : public Bar<Asd<N>>{
    void doSomething();
};

template<int N>
inline void Asd<N>::doSomething() {
    this->template setNumber(N);
}

int main() {
    Asd<42> obj;
    obj.doSomething();
}

被 GCC(甚至 13.1 和 trunk)接受,但被 clang 拒绝( https://godbolt.org/z/xbs8GqG7h )。 接受它似乎是错误的(setNumber 不是模板函数),所以我相信这是 GCC 中的一个错误。有人可以引用章节和诗句来明确这一点吗?

我尝试在 GCC bugzilla 中搜索“消歧器”,但只得到不相关的错误。

最佳答案

[temp.names]/6 :

A name prefixed by the keyword template shall be followed by a template argument list or refer to a class template or an alias template.

这里是名称setNumber

  • 后面没有模板参数列表;
  • 不引用类模板;
  • 不引用别名模板。

所以接受这个语法是错误的。

关于c++ - 非模板函数接受模板消歧器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76500060/

相关文章:

c++ - 当一个函数接受多个参数时如何组合 2 个函数?

c++ - 生成代码(在编译时)以调用模板的每个实例化的静态函数

c++ - OpenCV 与 QT 的集成

c++ - 如何将 C++ 中的可删除文本输出到控制台?

c++ - 如何准确定位程序崩溃的位置

c++ - boost::my_map_list_of

c++ - 为什么不允许全局的 struct/union 子项作为模板引用参数 - 但全局变量本身?

c++ - QTabWidget 中的 Qt4 QLabel

java - 在 C++ 中基于合法的函数参数声明局部数组

c++ - 为嵌套自定义类型定义散列函数