【发布时间】:2019-11-05 06:46:58
【问题描述】:
以下代码在 GCC 下编译正常,但在 clang 中失败并出现错误:
'Bar'的初始化没有匹配的构造函数
问题似乎是clang认为Foo的模板构造函数被Bar的模板构造函数隐藏或覆盖了。
这是 clang 中的错误还是 GCC 中的非标准化功能?
如何解决这个问题?我无法更改 Foo,因为它是第 3 方。
#include <type_traits>
struct Foo {
Foo() = default;
template<typename T, std::enable_if_t<std::is_trivially_copyable_v<T>>* = nullptr>
Foo(T& object) {}
};
struct Bar : public Foo {
using Foo::Foo;
template<typename T, std::enable_if_t<!std::is_trivially_copyable_v<T>>* = nullptr>
Bar(T& object) {}
};
int main() {
int i;
Bar s{i};
}
【问题讨论】:
-
您的
Bar构造函数只接受非平凡的数据类型,并且您传递给它一个整数。我错过了什么吗? -
但是我用
using Foo::Foo;继承了Foo构造函数。 -
@NutCracker - 从
Foo继承的构造函数
标签: c++ gcc clang c++17 sfinae