本文介绍了嵌套类和ADL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
代码如下:
namespace Namespace
{
struct L0
{
enum SomeEnum
{
EnumVal
};
struct L1
{
friend void f(SomeEnum)
{
std::cout << "f()" << std::endl;
}
};
friend void g(SomeEnum)
{
std::cout << "g()" << std::endl;
}
};
}
int main()
{
f(Namespace::L0::EnumVal); // error: f not defined
g(Namespace::L0::EnumVal); // good
}
这里的想法是使编译器通过ADL找到f()和g().
The idea here is to make the compiler find f() and g() through ADL.
但是,此代码无法使用gcc或clang编译.不过,类似的代码在MSVC下似乎可以很好地编译.
However, this code fails to compile with gcc or clang. The similar code seemed to compile fine under MSVC though.
也许我错过了一些东西,但是我真的不了解代码有什么问题,以及它是否有问题.如果有人可以对此有所启发,那就太好了.
Maybe I miss something, but I don't really understand what is wrong with the code, and whether it is wrong at all. Would be nice if someone could shed some light on this one.
PS.新年快乐:)
推荐答案
SomeEnum不是L1的成员,因此ADL找不到L1中定义的函数.
SomeEnum is not a member of L1, so ADL does not find a function defined within L1.
我相信,这是您想要的报价:
I believe, this is a quote you were looking for:
这篇关于嵌套类和ADL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!