小编flo*_*ian的帖子

C++ 奇怪的对重载函数的不明确调用

首先,这个问题纯粹是理论性的。我不是在寻找解决方案(我已经知道了),我只是在寻找解释。

以下代码无法编译:

struct foo {};
void a(foo) {}
namespace foobar {
    void a(foo) {}
    void b(foo f) {a(f);}
}
int main() {return 1;}
Run Code Online (Sandbox Code Playgroud)

MSVC++:

1>c:\projects\codetests\main.cpp(7) : error C2668: 'foobar::a' : ambiguous call to overloaded function
1>        c:\projects\codetests\main.cpp(4): could be 'void foobar::a(foo)'
1>        c:\projects\codetests\main.cpp(2): or       'void a(foo)' [found using argument-dependent lookup]
1>        while trying to match the argument list '(foo)'
Run Code Online (Sandbox Code Playgroud)

G++:

main.cpp: In function 'void foobar::b(foo)':
main.cpp:5:20: error: call of overloaded 'a(foo&)' is ambiguous
main.cpp:5:20: note: candidates are:
main.cpp:4:7: note: void …
Run Code Online (Sandbox Code Playgroud)

c++ namespaces compiler-theory ambiguous

5
推荐指数
1
解决办法
3035
查看次数

标签 统计

ambiguous ×1

c++ ×1

compiler-theory ×1

namespaces ×1