Han*_*Han 7 c++ gcc clang language-lawyer c++11
#include <iostream>
#include <utility>
struct B {
template<typename T, std::enable_if_t<std::is_same<T, int>::value>* = nullptr>
void foo(T) {
std::cout<<"B::foo"<<std::endl;
}
};
struct D: B {
using B::foo;
template<typename T, std::enable_if_t<std::is_same<T, std::string>::value>* = nullptr>
void foo(T) {
std::cout<<"D::foo"<<std::endl;
}
};
int main() {
D d;
d.foo(2); // gcc: print "B::foo"; clang: compile error
return 0;
}
Run Code Online (Sandbox Code Playgroud)
假设我们想foo()在派生类D中公开两个重载.gcc和Visual Studio编译并按照我的预期打印"B :: foo".但我得到一个与clang编译错误:
prog.cc:22:7: error: no matching member function for call to 'foo'
d.foo(2);
~~^~~
prog.cc:14:10: note: candidate template ignored: requirement 'std::is_same<int, std::string>::value' was not satisfied [with T = int]
void foo(T) {
Run Code Online (Sandbox Code Playgroud)
这是一个铿锵的bug吗?谢谢!
我实际上认为这是一个gcc bug(84832是密切相关的,尽管Wakely指出这可能是一个核心语言问题).
当using-declarator将基类的声明带入派生类时,派生类中的成员函数和成员函数模板覆盖和/或隐藏具有相同名称的成员函数和成员函数模板,parameter-type-list,cv- 基类中的限定和ref限定符(如果有)(而不是冲突).这些隐藏或重写的声明被排除在using-declarator引入的声明集之外.
其中一个参数类型列表中定义[DCL/FCT]/5:
使用以下规则确定函数的类型.每个参数的类型(包括函数参数包)由其自己的decl-specifier-seq和声明符确定.在确定每个参数的类型之后,将"T数组"或函数类型T的任何参数调整为"指向T的指针".生成参数类型列表后,在形成函数类型时,将删除修改参数类型的任何顶级cv限定符.生成的已转换参数类型列表以及省略号或函数参数包的存在与否是函数的parameter-type-list.
B::foo并D::foo具有相同的名称("foo"),parameter-type-list([T]),cv-qualification(none)和ref-qualifier(none).因此,派生的隐藏了基础的一个.
| 归档时间: |
|
| 查看次数: |
110 次 |
| 最近记录: |