问题描述
我今天遇到一个问题,即ADL找不到在类内部定义的类型的静态成员函数。
I had an issue today where ADL wasn't finding a static member function for a type defined inside a class.
在下面的示例中, str(foo :: Foo :: Enum)
在没有显式作用域的情况下不是通过ADL定位的, foo :: Foo :: str(foo :: Foo :: Enum)
That is, in the below example, str(foo::Foo::Enum)
isn't located via ADL without explicitly scoping it, foo::Foo::str(foo::Foo::Enum)
namespace foo {
struct Foo
{
enum Enum
{
FOO1,
FOO2
};
static const char* str(Enum e);
};
}
foo::Foo::Enum e = foo::Foo::FOO1;
const char* s = str(e); // ADL doesn't work
我发现 SO问题,并按照接受的答案所述,将其更改为 friend
函数导致ADL现在可以工作。
I found this SO question, and as stated in the accepted answer, changing it to a friend
function results in ADL now working.
namespace foo {
struct Foo
{
enum Enum
{
FOO1,
FOO2
};
friend const char* str(Enum e); // note str is now a friend
};
}
foo::Foo::Enum e = foo::Foo::FOO1;
const char* s = str(e); // ADL works now
虽然这现在对ADL有所帮助,但我惊讶地发现自己无法通过使用命名空间 foo
Whilst this now helps ADL, I was surprised to find that I couldn't access str
by scoping it with a namespace foo
foo::Foo::Enum e = foo::Foo::FOO1;
const char* s = foo::str(e); // error: ‘str’ is not a member of ‘foo’
我运行了一个测试,其中我打印了 __ PRETTY_FUNCTION __
的结果,更惊讶地发现str的范围显然是 foo ::
:
I ran a test, where I printed out the result of __PRETTY_FUNCTION__
, and was even more surprised to see that the scope of str is apparently foo::
:
__PRETTY_FUNCTION__: const char* foo::str(foo::Foo::Enum)
以下工作示例:
#include <iostream>
namespace foo {
struct Foo
{
enum Enum
{
FOO1,
FOO2
};
friend const char* str(Enum e)
{
return __PRETTY_FUNCTION__;
}
};
}
int main()
{
foo::Foo::Enum e = foo::Foo::FOO1;
std::cout << str(e) << '\n';
// std::cout << foo::str(e) << '\n'; // error: ‘str’ is not a member of ‘foo’
return 0;
}
输出:
$ ./a.out
const char* foo::str(foo::Foo::Enum)
问题:
- 为什么我无法找到
str(..)
明确地用封闭的名称空间对其进行范围界定? - 为什么
__ PRETTY_FUNCTION __
说它在foo ::
中,但是我却找不到它吗?
- Why am I unable to locate
str(..)
explicitly scoping it with the enclosing namespace? - Why does
__PRETTY_FUNCTION__
say it's infoo::
, and yet I am unable to locate it as such?
推荐答案
根据标准
这意味着 str
不可见命名查找;
That means str
is not visible to name lookup; it can only be called via ADL.
来自,
str
确实成为命名空间 foo
的成员;
str
does become member of namespace foo
; it's just invisible.
:
这篇关于无法显式访问名称空间范围的朋友的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!