Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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++ 函数模板专门化(以int值为模板)_C++_Templates - Fatal编程技术网

C++ 函数模板专门化(以int值为模板)

C++ 函数模板专门化(以int值为模板),c++,templates,C++,Templates,我使用的是线性代数软件包,它们提供了维度上的矩阵模板。我希望有一些为我的程序生成测试数据的函数,因此我也尝试在维度上对它们进行模板化,并具有特定的专业性: template <size_t K> boost::shared_ptr<std::vector<DistParams<K> > > sampleDistParams() { throw "not implemented"; } template <size_t K> boost

我使用的是线性代数软件包,它们提供了维度上的矩阵模板。我希望有一些为我的程序生成测试数据的函数,因此我也尝试在维度上对它们进行模板化,并具有特定的专业性:

template <size_t K>
boost::shared_ptr<std::vector<DistParams<K> > > sampleDistParams()
{
throw "not implemented";
}

template <size_t K>
boost::shared_ptr<std::vector<DistParams<K> > > sampleDistParams<1>()
{
boost::shared_ptr<std::vector<DistParams<K> > > distParams=(new std::vector<DistParams<K> >());
distParams->push_back(DistParams<K>(asMatrix(0.05), asMatrix(0.1)));
distParams->push_back(DistParams<K>(asMatrix(-0.1), asMatrix(0.2)));
return distParams;
}

template <size_t K>
boost::shared_ptr<std::vector<DistParams<K> > > sampleDistParams<2>()
{
boost::shared_ptr<std::vector<DistParams<K> > > distParams=(new std::vector<DistParams<K> >());
Eigen::Vector2d highMu;
lowMu << 0.04 << 0.06;
Eigen::Matrix2d lowSigma;
highSigma << 0.1 << 0.4
          << 0.4 << 0.12;

    Eigen::Vector2d lowMu;
lowMu << -0.08 << -0.12;
Eigen::Matrix2d highSigma;
highSigma << 0.2 << 0.7
          << 0.7 << 0.24;

distParams->push_back(DistParams<K>(highMu, lowSigma));
distParams->push_back(DistParams<K>(lowMu, highSigma));
return distParams;
}
模板
boost::sharedptrsampledistparams()
{
抛出“未执行”;
}
模板
boost::sharedptrsampledistparams()
{
boost::shared_ptr distParams=(新std::vector());
distParams->push_back(distParams(asMatrix(0.05),asMatrix(0.1));
distParams->push_back(distParams(asMatrix(-0.1),asMatrix(0.2));
返回参数;
}
模板
boost::sharedptrsampledistparams()
{
boost::shared_ptr distParams=(新std::vector());
本征::矢量2D高μ;

lowMu如果要专门化函数模板,则不应重新声明模板参数(请注意,不能部分专门化函数模板):

模板void foo(){}//主模板
模板void foo(){}//错误:重新定义
模板void foo(){}//确定:专门化
在您的特定情况下,模板专门化的正确签名是:

template<>
boost::shared_ptr<std::vector<DistParams<1> > > sampleDistParams<1>()
模板
boost::sharedptrsampledistparams()

另外请注意,在C++11中,右尖括号(
)之间的空格不再是必需的。

如果要专门化函数模板,则不应重新声明模板参数(请注意,不能部分专门化函数模板):

模板void foo(){}//主模板
模板void foo(){}//错误:重新定义
模板void foo(){}//确定:专门化
在您的特定情况下,模板专门化的正确签名是:

template<>
boost::shared_ptr<std::vector<DistParams<1> > > sampleDistParams<1>()
模板
boost::sharedptrsampledistparams()
另外请注意,在C++11中,右尖括号(
)之间的空格不再是必需的。

错误语法:

模板专门化应为:

template <>
boost::shared_ptr<std::vector<DistParams<K> > > sampleDistParams<1>()
模板
boost::sharedptrsampledistparams()
语法错误:

模板专门化应为:

template <>
boost::shared_ptr<std::vector<DistParams<K> > > sampleDistParams<1>()
模板
boost::sharedptrsampledistparams()