Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在C++;_C++_Class_C++11_Templates - Fatal编程技术网

C++ 在C++;

C++ 在C++;,c++,class,c++11,templates,C++,Class,C++11,Templates,有没有办法在另一个类中设置模板类的模板参数?我想有一个类,它生成具有两个值的特定类型的分布(正态分布、均匀分布等)。该类的名称应如下所示: Dist normal("normal",0,1) // this should construct std::normal_distribution<double> normal(0,1); Dist uniform("uniform",1,10); //std::uniform_real_distribution<double>

有没有办法在另一个类中设置模板类的模板参数?我想有一个类,它生成具有两个值的特定类型的分布(正态分布、均匀分布等)。该类的名称应如下所示:

Dist normal("normal",0,1) // this should construct std::normal_distribution<double> normal(0,1);
Dist uniform("uniform",1,10); //std::uniform_real_distribution<double> uniform(1,10);
distnormal(“normal”,0,1)//这应该构造std::normal\u分布正态(0,1);
地区制服(“制服”,1,10)//标准:均匀实分布均匀(1,10);
一种方法是使
Dist
类也成为模板类。但是我想
Dist
成为一个非模板类。原因是我有另一个类,它应该获取和std::vector of
Dist
作为输入(
std::vector
)。如果
Dist
是一个模板类,则无法执行此操作

但我想知道,是否有可能做到这一点,上面所示的方式

是的,但我不明白你为什么要这么做。您需要使用某种运行时“字符串到工厂”映射和类型擦除

用法:

Vector<int> IntVec(1, 2);
Vector<double> DoubleVec(1.0, 2.0);
向量IntVec(1,2); 向量双向量(1.0,2.0);
谢谢!我编辑了我的问题。我希望现在我想做的事情更清楚了。
template <typename T>
struct Vector
{
    std::vector<T> _v;

    template <typename... Ts>
    Vector(Ts&&... xs) : _v{std::forward<Ts>(xs)...} { }
};
Vector<int> IntVec(1, 2);
Vector<double> DoubleVec(1.0, 2.0);