c++ - SFINAE 不适用于复制构造函数
问题描述
我有一个模板类,我想有两个复制 ctor。一个用于平凡类型,另一个用于非平凡类型。以下代码有效(使用一个副本 ctor):
template <typename T>
struct MyStruct
{
MyStruct()
{}
MyStruct(const MyStruct& o)
{
std::cout << "copy ";
foo(o);
}
template <typename U = T, typename std::enable_if_t<!std::is_trivial<U>::value, int> =0>
void foo(const MyStruct& o)
{
std::cout << "Non trivial" << std::endl;
}
template <typename U = T, typename std::enable_if_t<std::is_trivial<U>::value, int> =0>
void foo(const MyStruct& o)
{
std::cout << "Trivial" << std::endl;
}
MyStruct(MyStruct&& o)
{
std::cout << "Move" << std::endl;
}
};
struct MyType
{
MyType(int i){}
};
int main()
{
MyStruct<int> my1;
MyStruct<int> my2(my1);
MyStruct<MyType> mytype1;
MyStruct<MyType> mytype2(mytype1);
}
// prints
// Copy Trivial
// Copy Non trivial
当我尝试对两个复制 ctor 做同样的事情时,似乎它们都没有生成(两者都被 SFINA 删除了)
struct MyStruct
{
MyStruct()
{}
template <typename U = T, typename std::enable_if_t<!std::is_trivial<U>::value, int> =0>
MyStruct(const MyStruct& o)
{
std::cout << "Non trivial" << std::endl;
}
template <typename U = T, typename std::enable_if_t<std::is_trivial<U>::value, int> = 0>
MyStruct(const MyStruct& o)
{
std::cout << "Trivial" << std::endl;
}
MyStruct(MyStruct&& o)
{
std::cout << "Move" << std::endl;
}
};
代码无法编译:
main.cpp: In function ‘int main()’:
main.cpp:36:26: error: use of deleted function ‘constexpr MyStruct::MyStruct(const MyStruct&)’
MyStruct<int> my2(my1);
^
main.cpp:5:9: note: ‘constexpr MyStruct::MyStruct(const MyStruct&)’ is implicitly declared as deleted because ‘MyStruct’ declares a move constructor or move assignment operator
struct MyStruct
^~~~~~~~
main.cpp:39:37: error: use of deleted function ‘constexpr MyStruct::MyStruct(const MyStruct&)’
MyStruct<MyType> mytype2(mytype1);
^
main.cpp:5:9: note: ‘constexpr MyStruct::MyStruct(const MyStruct&)’ is implicitly declared as deleted because ‘MyStruct’ declares a move constructor or move assignment operator
struct MyStruct
^~~~~~~~
我试图用谷歌搜索它,我认为 SFINAE 也应该与复制构造函数一起使用。请不要建议使用if constexpt
or requires concept
,这个问题是关于 SFINAE 的。谢谢你的帮助!
解决方案
不幸的是,模板构造函数不能是默认、复制或移动构造函数。
复制构造函数的签名MyStruct
如下
MyStruct (MyStruct const &);
在您的第二个示例中,此函数被隐式删除,因为您编写了一个显式移动构造函数。
您的模板构造函数,具有以下签名,
template <typename, int>
MyStruct (MyStruct const &);
是(是)一个有效的构造函数,但是,给定的是一个模板,(不幸地删除了)真正的复制构造函数是首选。
可能的解决方案:通过附加参数添加一个调用(委托构造函数)模板构造函数的真实复制构造函数。
我是说
template <typename T>
struct MyStruct
{
MyStruct()
{}
// delegating real copy constructor added
MyStruct(const MyStruct & ms0) : MyStruct{ms0, 0}
{ }
template <typename U = T,
std::enable_if_t<!std::is_trivial<U>::value, int> = 0>
MyStruct(const MyStruct& o, int = 0) // <-- added an int argument
{ std::cout << "Non trivial" << std::endl; }
template <typename U = T,
std::enable_if_t<std::is_trivial<U>::value, int> = 0>
MyStruct(const MyStruct& o, int = 0) // <-- added and int argument
{ std::cout << "Trivial" << std::endl; }
MyStruct(MyStruct&& o)
{ std::cout << "Move" << std::endl; }
};
推荐阅读
- docker - 如何授予 Openshift 容器的非特权用户对 /root/.ssh 的读取权限以进行 Spring Cloud Config Server SSH 身份验证?
- ios - PostGraphile 和 Swift (SwiftUI) 集成
- python - 为什么虽然触发了视图,但评论对象没有从数据库中删除?
- python - Python pandas:为什么 `del df.loc[:,column_name]` 不起作用?(即使 `del df[column_name]` 可以?
- angular - 如何更改引导轮播项目Angular
- java - 使用 Spring Autowired 注解的要求
- javascript - JSON.stringify 将空对象从 FileList 发送到服务器
- elasticsearch - EKS EFK 日志记录方法
- amazon-web-services - 如何使用 Cloudwatch 将常量值传递给 AWS Step Function
- powershell - 如何从csv文件循环遍历层次结构?