在解析基类的模板化成员函数的重载期间,我观察到g ++(5.2.1-23)和clang(3.8.0)之间的不同行为-std=c++14
.
#include#include struct Base { template auto a(T t) -> void { std::cout<< "False\n"; } }; template struct Derived : public Base { using Base::a; template auto a(T t) -> std::enable_if_t { std::cout<< "True\n"; } }; int main() { Derived d; d.a(1); // fails with g++, prints "true" with clang Derived d2; d2.a(1); // fails with clang++, prints "false" with g++ }
Derived
使用g ++ 调用失败并显示以下消息:
test.cc: In function ‘int main()’: test.cc:28:8: error: call of overloaded ‘a(int)’ is ambiguous d.a(1); ^ test.cc:18:8: note: candidate: std::enable_if_t Derived::a(T) [with T = int; bool B = true; bool Bool = true; std::enable_if_t = void] auto a(T t) -> std::enable_if_t ^ test.cc:7:8: note: candidate: void Base::a(T) [with T = int] auto a(T t) -> void { ^
并Derived
使用以下消息使用clang ++ 调用失败:
test.cc:32:6: error: no matching member function for call to 'a' d2.a(1); ~~~^ /usr/bin/../lib/gcc/x86_64-linux-gnu/5.2.1/../../../../include/c++/5.2.1/type_traits:2388:44: note: candidate template ignored: disabled by 'enable_if' [with T = int, B = false] using enable_if_t = typename enable_if<_Cond, _Tp>::type; ^
我的猜测是,他们对诠释的解释不同using Base::a;
,而且在俚语中不予考虑,而在g ++中则考虑(可能太多).我认为会发生的是,如果Derived
有true
参数,则调用a()
调度到Derived
的实现,而如果参数是false
,则调度调度Base::a
.
他们都错了吗?谁是对的?我应该向谁提交错误报告?有人可以解释发生了什么吗?
谢谢