gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:00:33 35 4
gpt4 key购买 nike

关于 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/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com