【问题标题】:Function template specialization (templated on an int value)函数模板特化(以 int 值为模板)
【发布时间】:2013-02-13 02:40:50
【问题描述】:

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

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

但是,这些东西不能编译。我明白了:

/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

出了什么问题,我该如何解决?

【问题讨论】:

    标签: c++ templates


    【解决方案1】:

    如果你想特化一个函数模板,你不应该重新声明模板参数(注意你不能部分特化一个函数模板):

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

    在您的特定情况下,模板专业化的正确签名是:

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

    还要注意,在 C++11 中,右尖括号 (&gt;) 之间的空格不再需要。

    【讨论】:

      【解决方案2】:

      语法错误:

      模板特化应该是:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多