基于模板的动态数据类型选择
Dynamic data type choosing based on template
假设我有这样一个对象:
class Spline {
public:
Spline(std::size_t const dim);
// Quite a few functions here. One of those:
vec operator()(double const t) const; // Returns vector of dimension d
}
现在,在这个类的大多数使用中,维数将在编译时确定,因此(出于性能原因)像这样更改类是一个好主意:
template <std::size_t dim>
class Spline {
public:
Spline();
// Quite a few functions here. One of those:
vec::fixed<dim> operator()(double const t) const; // Returns vector of dimension d
}
(vec
和vec::fixed
是由犰狳线性代数库定义的对象)。现在我想有两个版本生活在并行,从而能够选择维度在编译时以及在运行时。简而言之,我想创建vec::fixed<dim>
与Spline::fixed<dim>
的等价物,但不需要两次实现所有功能。特别是,我必须根据是否存在模板参数来选择所有这些函数的返回类型。
你知道我该如何做到这一点吗,特别是考虑到一个清晰和可维护的设计?(希望我把自己说得很清楚,虽然我不是很确定)
使用一个简单的traits元结构并特殊化它。
template<std::size_t dim>
struct spline_return_traits{
typedef vec::fixed<dim> type;
};
template<>
struct spline_return_traits<0>{ // 0 is a special marker for runtime dimensions
typedef vec type;
};
template<std::size_t dim>
class Spline_impl{
typedef typename spline_return_traits<dim>::type spline_return;
public:
spline_return operator()(double const t) const;
// if <dim> is 0, then the dynamic vec will be chosen as the return type
// all your functions
};
class Spline : public Spline_impl<0>{ // default is dynamic
public:
template<int dim>
struct fixed : public Spline_impl<dim>{
};
};
现在你简单地使用它。Spline_impl
的每个操作符、构造函数和函数都应该在子类中可用。对于每个函数的实现,您需要做一些分支,必须在运行时或固定vec
之间做出决定:
if(dim == 0){
// runtime specific stuff
}else{
// compile-time specific stuff
}
使用:
Spline dynamic_spline;
Spline::fixed<10> fixed_10_spline;
唯一的问题是Spline
类的大小将是Spline_impl
的两倍…:/让我想想是否也能找到解决办法。
编辑:如果您不希望Spline
的大小是Spline_impl
的两倍,一种可能性是添加一点冗长和typedef:
class Spline : public Spline_impl<0>{ // default is dynamic size
public:
template<std::size_t dim>
struct fixed{
typedef Spline_impl<dim> type;
};
};
和as
Spline dynamic_spline;
typename Spline::fixed<10>::type fixed_10_spline;
如果我正确理解你的问题,你想要一个struct
的编译时使用和运行时使用以及最好的名称相同。在我看来,您可以将class
声明为template
,然后专门化它的一个实例(例如size_t = 0xffffffff),您可能不会使用该实例。您可以在该实例中声明所有用于运行时使用的定义。
template<std::size_t dim = ~0> // choose default dimension(0xffffffff) not to be used
class Spline {
public:
Spline () {}
vec::fixed<dim> operator () (double const t) const {}
};
template<>
class Spline<~0> { // specialize the default dimension for runtime use
public:
Spline (std::size_t const dim) {}
vec operator () (double const t) const {}
};
可以这样使用:
Spline<5> o5; // compile time dimensions
Spline<> o0(3); // run time dimensions (don't mention anything in template)
你可以重载它。然而,要实现一个不重复的实现是一个没有通用解决方案的问题,除非您有更多的模板可以做到这一点。
相关文章:
- 防止主数据类型C++的隐式转换
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 在C++中打印指向不同基元数据类型的指针的内存地址
- C++浮点数据类型和字符串数据类型无法子到模板函数中
- 如何计算数据类型的范围,例如int
- C++中数据类型修饰符的顺序
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数
- 特定数据类型的模板类
- 具有多个模板的模板函数,用于特定数据类型(如字符串)?
- 有没有办法提示用户使用哪种数据类型作为模板 c++
- int数据类型的指针指向的是什么,如果是一个类的私有数据成员,我们创建了该类的两个对象?
- 时间复杂度 当具有复合数据类型(如元组或对)时?
- 如何获取C++字符数据类型的地址
- 如何为我的数组选择更大的数据类型?
- 当您在C中定义一个值时,编译器如何选择数据类型
- 在main()中为通用模板类选择数据类型
- 根据大小选择数据类型
- 关于在c++中使用模板特性来选择数据类型
- 如何根据要写入的数据类型动态选择文件句柄
- 基于模板的动态数据类型选择