如果给定模板参数,则使用sfinae启用构造函数提供类型定义
enabling a constructor with sfinae if given template argument provides a type definition
根据给定类型作为模板形参是否定义了类型,我希望在模板类中启用两个构造函数中的一个且仅一个。
这里,我想在构造函数中添加来自给定模板的定义类型作为附加参数,如果存在该参数,则转发该参数。但见:
class A
{ // this class did not provide ConstructorParms type definition
public:
A( int i) {}
};
class B
{ // this class provide the type ConstructorParms
public:
using ConstructorParms= double;
B( double d, int i) {}
};
template <typename T>
class Check: public T
{
public:
// ??? enable_if T has typename T::ConstructorParms
Check( typename T::ConstructorParms parms, int i): T( parms, i) {}
// ??? enable_if T has NOT typename T::ConstructorParms
Check( int i): T(i){}
};
int main()
{
Check<A> ca(9);
Check<B> cb(1.23, 10);
return 0;
}
我遇到了麻烦,因为构造函数"method"本身没有模板parm。任何想法?
注意(给出答案的原因):代码片段确实简化了。用using转发给父构造函数是不可能的,因为现实世界中的类Check需要一些参数作为它们自己的参数。这使得转发到父构造函数是不可能的。
你可以使用SFINAE来解决这个问题,但自c++ 11以来,有一个更简单的解决方案:继承的构造函数。
template <typename T>
class Check: public T
{
public:
using T::T;
};
如果构造函数不仅仅做转发;使用以下通用解决方案:
#include <type_traits>
template< typename T >
struct HasCtorParamTypedef
{
template< typename U,
typename = typename U::ConstructorParms >
static int f( U );
static void f( ... );
static const bool value = std::is_same<decltype(f(std::declval<T>())), int>::value;
};
template <typename T>
struct Check: public T
{
// Is only enabled if T does have the typedef
template <typename U = T,
typename = typename std::enable_if<HasCtorParamTypedef<U>::value>::type >
Check( typename U::ConstructorParms parms, int i): T( parms, i) {}
// Is only enabled if T does **not** have the typedef
template <typename U = T,
typename = typename std::enable_if<!HasCtorParamTypedef<U>::value>::type >
Check( int i ): T(i){}
};
相关文章:
- "error: no matching function for call to"构造函数错误
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 选择要调用的构造函数
- 如何委托派生类使用其父构造函数?
- 构造函数正在调用一个使用当前类类型的函数
- 有条件地启用类C++构造函数
- 如何启用默认构造函数?
- SFINAE enable_if 使用 pre c++11 启用构造函数
- 如何仅在给定n个以上参数的情况下启用可变模板构造函数_if
- 为什么 g++ 不发出这个与构造函数相关的 noexcept 启用"-fpic"的警告?
- 启用默认初始值设定项列表构造函数
- 如何为显式重载构造函数启用复制初始化
- C++模板添加/启用基于模板参数的构造函数
- 如果给定模板参数,则使用sfinae启用构造函数提供类型定义
- Clang:如何启用ext_vector_type的vector构造函数
- 启用类构造函数
- 启用基于类模板形参的构造函数时,是否总是需要复制该类模板形参?