在不专门针对每个类模板/类的情况下,是否可以编写一个通用的“重新绑定(bind)”元函数,以便 给予
template<class > struct foo;
struct bar;
以下
is_same<rebind<foo<int>,float>,foo<float>>
is_same<rebind<bar>,bar>
也许
is_same< rebind<std::vector<int>,float>,std::vector<float>>
返回一个等价于 true 的类型?
最佳答案
当然可以。
但请注意,任何采用可变模板参数列表的模板模板参数仅限于接受仅具有类型参数的模板,而不是非类型参数。换句话说,下面的一般情况不适用于 std::array
,因为它的第二个参数是一个整数。您必须添加一个特例。
主模板已经是一个特例,因为它处理的类不是模板的特化。
http://liveworkspace.org/code/5b6f0cb3aec1eb74701e73d8d21aebab
template< typename bound, typename ... new_args >
struct rebind_class {
static_assert( sizeof ...( new_args ) == 0,
"can't rebind arguments to non-specialization" );
typedef bound type;
};
template< template< typename ... > class template_, typename ... new_args,
typename ... old_args >
struct rebind_class< template_< old_args ... >, new_args ... > {
typedef template_< new_args ... > type;
};
template< typename ... args >
using rebind = typename rebind_class< args ... >::type;
关于c++ - 是否可以编写通用的重新绑定(bind)模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13284340/