简体   繁体   English

函数模板特化(在int值上模板)

[英]Function template specialization (templated on an int value)

I am using Eigen linear algebra package, where they provide matrices templated on the dimensions. 我使用的是Eigen线性代数软件包,它们在维度上提供模板化的矩阵。 I would like to have some functions that generate test data for my program, and so I am trying to template them on the dimensions as well, and have specific specialisations: 我想要一些可以为我的程序生成测试数据的函数,因此我也尝试在维度上对它们进行模板化,并具有特定的专业知识:

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;
}

however, this stuff doesn't compile. 但是,这些东西无法编译。 I get: 我得到:

/home/ga1009/PhD/cpp/pmi/cpp/test/baumiterationtest.cpp:24:73: error: template-id ‘sampleDistParams<1>’ in declaration of primary template
/home/ga1009/PhD/cpp/pmi/cpp/test/baumiterationtest.cpp:24:53: error: redefinition of ‘template<unsigned int K> boost::shared_ptr<std::vector<DistParams<K> > > {anonymous}::sampleDistParams()’
/home/ga1009/PhD/cpp/pmi/cpp/test/baumiterationtest.cpp:18:53: error: ‘template<unsigned int K> boost::shared_ptr<std::vector<DistParams<K> > > {anonymous}::sampleDistParams()’ previously declared here
/home/ga1009/PhD/cpp/pmi/cpp/test/baumiterationtest.cpp:33:73: error: template-id ‘sampleDistParams<2>’ in declaration of primary template
/home/ga1009/PhD/cpp/pmi/cpp/test/baumiterationtest.cpp:33:53: error: redefinition of ‘template<unsigned int K> boost::shared_ptr<std::vector<DistParams<K> > > {anonymous}::sampleDistParams()’
/home/ga1009/PhD/cpp/pmi/cpp/test/baumiterationtest.cpp:18:53: error: ‘template<unsigned int K> boost::shared_ptr<std::vector<DistParams<K> > > {anonymous}::sampleDistParams()’ previously declared here

What went wrong and how can I fix it? 出了什么问题,我该如何解决?

If you want to specialize a function template, you should not redeclare the template parameters (note that you cannot partially specialize a function template): 如果要专门化功能模板,则不应重新声明模板参数(请注意,您不能部分地专门化功能模板):

template<size_t K> void foo() { } // Primary template

template<size_t K> void foo<1>() { } // ERROR: Redefinition
template<> void foo<1>() { } // OK: Specialization

In your particular case, the right signature for the template specialization is: 在您的特定情况下,模板专业化的正确签名是:

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

Also notice, that in C++11 the spaces between the closing angle brackets ( > ) are no more necessary. 还要注意,在C ++ 11中,在尖括号( > )之间不再需要空格。

wrong syntax: 错误的语法:

template specialization should be: 模板专业化应为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM