c++ - 这是 MSVC 中依赖名称解析的错误吗?

标签 c++ visual-c++ compiler-bug dependent-name

关于 cppreference.com , 以下代码作为解释相关名称解析的示例提供:

#include <iostream>
void g(double) { std::cout << "g(double)\n"; }

template<class T>
struct S {
    void f() const {
        g(1); // "g" is a non-dependent name, bound now
    }
};

void g(int) { std::cout << "g(int)\n"; }

int main()
{
    g(1); // calls g(int)

    S<int> s;
    s.f(); // calls g(double)
}

当前版本的 Visual C++ (19.0.23918.0) 产生以下输出:

g(int)
g(int)

这是标准允许的,还是 MSVC 中的错误?

最佳答案

“从属名称解析”在这里具有误导性。 g 是一个非依赖名称,因此适用的规则是 temp.nondep不是temp.dep.res :

Non-dependent names used in a template definition are found using the usual name lookup and bound at the point they are used. [ Example:

void g(double);
void h();

template<class T> class Z {
public:
  void f() {
    g(1);           // calls g(double)
    h++;            // ill-formed: cannot increment function;
                    // this could be diagnosed either here or
                    // at the point of instantiation
  }
};

void g(int);        // not in scope at the point of the template
                    // definition, not considered for the call g(1)

end example ]

这实际上与 cppreference 上的示例相同。所以是的,这是 MSVC 中的错误。

关于c++ - 这是 MSVC 中依赖名称解析的错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37703226/

相关文章:

c++ - 使用 Korge 编译 native 时抛出错误 "Unable to find library"

c++ - 函数指针(指向其他内核)作为 CUDA 中的内核 arg

c++ - 使用 BOOST::GIL 将 CMYK 图像转换为 RGB

c++ - MSVC : what compiler switches affect the size of structs?

c++ - 将 SFML 与 GitHub 上的 C++ 项目同步

C++/Cuda 模板

c++ - Template模板和CRTP : compiler bugs,和GCC和clang不一致

c++ - C++ 预处理器中的 R 和 L 有什么特别之处?

c++ - 通过调用 transform 方法对齐持有不同类型的两个容器

c++ - 英特尔 C++ 编译器 - const 字符串是可修改的