元功能重载C ++ - enable_if [英] metafunction overload C++ - enable_if
问题描述
假设我想要有两个名为multiplicate的元函数。这些元函数应该对向量类型进行操作。
-
一个元函数应该输入两个向量, >
我想要编译的代码:
int V2,int V3 ...>
struct vector_c {
enum {
v1 = V1,
v2 = V2,
v3 = V3,
///
}
};
template< typename Vector1,typename Vector2>
struct multiplicate {
typedef / * do stuff * / type;
};
template< typename Vector1,int value>
struct multiplicate {
typedef / * do stuff * / type;
};
事情是,这段代码不会编译。我想像做一些像:
模板< typename Vector1,typename Vector2,
typename enable_if_c< is_vector< Vector2> ; :: value,int> :: type = 0>
struct multiplicate {
typedef / * do stuff * / type;
}; //应该是罚款
template< typename Vector1,int value,
typename enable_if_c // what now? >
struct multiplicate {
// stuff
};
事情是,在第二种情况下,我不能把任何东西放到enable_if,如
值不是类型,但它已经是类型int的值。
您需要使用模板专业化,而不是两个不同的模板。
//主模板前进声明
模板< typename Vector1,typename Vector2,typename Enable = void>
struct multiplicate;
//当is_vector< Vector2>是true
//让enable_if的第二个参数为默认值!!!
template< typename Vector1,typename Vector2>
struct multiplicate< Vector1,Vector2,
typename enable_if< is_vector< Vector2> :: value> :: type>
{// do the stuf
};
//当Vector2完全是类型int
时的特殊化< typename Vector1,typename Vector2>
struct multiplical< Vector1,Vector2,
typename enable_if< is_same< Vector2,int> :: value> :: type>
{// do the stuf
};
/ *任何其他情况的声明!
当您注释(或删除),编译失败
其他类型的Vector2与错误:不完整类型* /
模板< typename Vector1,typename Vector2,typename启用>
struct multiplicate
{//做stuf
};
快乐编码!
Let's say I want to have 2 meta functions named multiplicate. Those metafunctions should operate on vector types.
One metafunction should take as input two vectors and multiply one value by another
Another one should take as input one vector and scalar and multiply all values in vector by scalar.
The code I would like to have compilabe:
template <int V1, int V2, int V3...>
struct vector_c{
enum{
v1 = V1,
v2 = V2,
v3 = V3,
///
};
};
template <typename Vector1, typename Vector2>
struct multiplicate{
typedef /* do stuff */ type;
};
template <typename Vector1, int value>
struct multiplicate{
typedef /* do stuff */ type;
};
The thing is, that this code won't compile. I thought of doing somehing like:
template <typename Vector1, typename Vector2,
typename enable_if_c<is_vector<Vector2>::value, int>::type =0>
struct multiplicate{
typedef /* do stuff */ type;
}; //should be fine
template <typename Vector1, int value,
typename enable_if_c // what now? >
struct multiplicate{
//stuff
};
The thing is, that in the second case I cannot put anything to enable_if, as value is not a type but it's already a value of type int. How can make this code working?
You need to use template specialization, not two different templates.
//Primary template forward declaration
template<typename Vector1, typename Vector2, typename Enable = void>
struct multiplicate;
//specialization when is_vector<Vector2> is true
//leave second argument of enable_if with default value!!!
template<typename Vector1, typename Vector2>
struct multiplicate<Vector1, Vector2,
typename enable_if<is_vector<Vector2>::value>::type>
{ //do the stuf
};
//specialization when Vector2 is exactly type int
template<typename Vector1, typename Vector2>
struct multiplicate<Vector1, Vector2,
typename enable_if<is_same<Vector2, int>::value>::type>
{ //do the stuf
};
/* Declaration for any other case!
when you comment it (or delete), compilation fails
for other types of Vector2 with error: incomplete type */
template<typename Vector1, typename Vector2, typename Enable>
struct multiplicate
{ //do the stuf
};
Happy coding!
这篇关于元功能重载C ++ - enable_if的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!