我想实现一个像std :: pair但有两个以上组件的类。由于在我的应用程序中可能会发生某些元组件在编译时已经知道,我想要进行以下空间优化:当我知道某个组件是编译时常量时,只需将其声明为“静态常量“成员,这样它就不会浪费存储在各个类实例中。 const限定符确保在运行时修改该值的任何尝试都会导致编译错误,至少如果我们排除不礼貌的const_cast(s)。编译时常量的优化
我结束了下面的实现,一个n元组类
template<typename T0_ = void,
typename T1_ = void,
typename T2_ = void,
typename T3_ = void
> class ntuple;
和一类用于标记编译时常
template<class type_, type_ value_> class constant
{
typedef type_ type;
static const type value = value_;
};
和一堆n元组类
的部分特例的template<typename T0_>
class ntuple<
T0_
> {
public:
static const int n=1;
typedef T0_ T0;
static const bool is_static = false;
static const bool is_static0 = false;
T0_ i0;
};
template<
typename T0_, T0_ value0_
>
class ntuple<
constant<T0_, value0_>
> {
public:
static const int n=1;
typedef T0_ T0;
static const bool is_static = true;
static const bool is_static0 = true;
static const T0_ i0 = value0_;
};
template<
typename T0_, T0_ value0_
> const T0_ ntuple<
constant<T0_, value0_> >::i0;
template<
typename T0_,
typename T1_
>
class ntuple<
T0_,
T1_
> {
public:
static const int n=2;
typedef T0_ T0;
typedef T1_ T1;
static const bool is_static = false;
static const bool is_static0 = false;
static const bool is_static1 = false;
T0_ i0;
T1_ i1;
};
template<
typename T0_,
typename T1_, T1_ value1_
>
class ntuple<
T0_,
constant<T1_, value1_>
> {
public:
static const int n=2;
typedef T0_ T0;
typedef T1_ T1;
static const bool is_static = false;
static const bool is_static0 = false;
static const bool is_static1 = true;
T0_ i0;
static const T1_ i1 = value1_;
};
template<
typename T0_,
typename T1_, T1_ value1_
> const T1_ ntuple<
T0_,
constant<T1_, value1_> >::i1;
template<
typename T0_, T0_ value0_,
typename T1_
>
class ntuple<
constant<T0_, value0_>,
T1_
> {
public:
static const int n=2;
typedef T0_ T0;
typedef T1_ T1;
static const bool is_static = false;
static const bool is_static0 = true;
static const bool is_static1 = false;
static const T0_ i0 = value0_;
T1_ i1;
};
template<
typename T0_, T0_ value0_,
typename T1_
> const T0_ ntuple<
constant<T0_, value0_>,
T1_ >::i0;
template<
typename T0_, T0_ value0_,
typename T1_, T1_ value1_
>
class ntuple<
constant<T0_, value0_>,
constant<T1_, value1_>
> {
public:
static const int n=2;
typedef T0_ T0;
typedef T1_ T1;
static const bool is_static = true;
static const bool is_static0 = true;
static const bool is_static1 = true;
static const T0_ i0 = value0_;
static const T1_ i1 = value1_;
};
template<
typename T0_, T0_ value0_,
typename T1_, T1_ value1_
> const T0_ ntuple<
constant<T0_, value0_>,
constant<T1_, value1_> >::i0;
template<
typename T0_, T0_ value0_,
typename T1_, T1_ value1_
> const T1_ ntuple<
constant<T0_, value0_>,
constant<T1_, value1_> >::i1;
这样标记为常量的成员<。,。>不作为类成员存储,因此减少了对象大小。需要的部分专业化的数量可能很大,对于N = 1,2,3,4,我只报告N = 2的2^N:我写了一个简单的脚本来生成所有这些。这个类可以使用如下:
ntuple<int, int, bool> tup1;
tup1.i0=2;
tup1.i1=0;
tup1.i2=true;
assert (tup1.i0==2);
assert (tup1.i1==0);
assert (tup1.i2==true);
ntuple<int, constant<int, 3>, constant<bool, false> > tup2;
tup2.i0=2;
// tup2.i1=0; // cannot be assigned, is static a constant
// tup2.i2=true; // cannot be assigned, is static a constant
assert (tup2.i0==2);
assert (tup2.i1==3);
assert (tup2.i2==false);
assert (sizeof(tup1)>sizeof(tup2));
像这样,这个类的作品完美。现在,我只是想提高n元组的声明 语法如下
ntuple<int, int_<3>, bool_<true> >
,而不是
ntuple<int, constant<int, 3>, constant<bool, true> >
其中int_和bool_可以定义为
template<int i> struct int_ : constant<int, i> {};
template<bool b> struct bool_ : constant<bool, b> {};
我也可以只需使用boost :: mpl类似物,这不是重点。为了达到这个目的,简单的解决方案是编写另一个脚本并为常量和非常量模板参数的所有排列生成所有可能的特化,其中常量模板参数可以是int_,bool_,char_等。这是可行的,但以成本部分专业化数量的因子增加。我想改变n元组类的定义如下
template<typename T0_ = void,
typename T1_ = void,
typename T2_ = void,
typename T3_ = void,
bool const0 = is_const<T0_>::value,
bool const1 = is_const<T1_>::value,
bool const2 = is_const<T2_>::value,
bool const3 = is_const<T3_>::value
> class ntuple;
与
template <class T> is_const { static const bool value = false; };
template <int i> is_const<int_<i> > { static const bool value = true; };
template <bool b> is_const<bool_<b> > { static const bool value = true; };
,并专注n元组如下
template<typename T0_,
typename T1_,
typename T2_,
typename T3_> class ntuple<T0_,T1_,T2_,T3_,false,false,false,false> { ... };
template<typename T0_,
typename T1_,
typename T2_,
typename T3_> class ntuple<T0_,T1_,T2_,T3_,true,false,false,false> { ... };
等。这将减少的部分数专业化到与之前相同的数量,并且只需要针对每个有效的“常量”类型对特征类进行专门化。问题是,我想避免额外的模板参数。我可以通过继承为此,限定一辅助类
template<typename T0_ = void,
typename T1_ = void,
typename T2_ = void,
typename T3_ = void,
bool const0 = is_const<T0_>::value,
bool const1 = is_const<T1_>::value,
bool const2 = is_const<T2_>::value,
bool const3 = is_const<T3_>::value
> class ntuple_impl;
如上专门然后
template <class T0, class T1, class T2, class T3>
class ntuple : ntuple_impl<T0, T1, T2, T3,
is_const<T0>::value,
is_const<T1>::value,
is_const<T2>::value,
is_const<T3>::value> { ... };
,但我想避免继承,因为所得到的物体将是比必要在一些情况下更大的因为它将包含ntuple_impl作为子对象。我会知道是否有另一个解决这个问题的方法。谢谢。朱利亚诺
你的下划线让我疯狂。此外,你确定这是一个优化的好方法吗?如果您需要脚本来生成它们,我认为您错过了整个模板点! – vulkanino 2012-03-02 13:53:06
由于我需要将关键字'静态'放在所选数据成员的前面,'静态'不是该类型的一部分,因此无法使用模板技巧来实现此目的。至少,我想......谢谢! – Giuliano 2012-03-02 14:13:40
我想你应该试着描述一下你想达到的目标,因为我认为这很可能有一个更简单的方法。 – 2012-03-02 14:40:57