【问题标题】:Template partial specialization problems模板偏特化问题
【发布时间】:2012-11-21 11:09:30
【问题描述】:

我正在尝试为数学编程编写一个大小和类型的通用向量类。我在部分专业化方面遇到问题。

当我尝试将向量类的成员方法专门化为给定大小时,就会出现问题。

我可以举一个简单的例子:

template <size_t Size, typename Type>
class TestVector
{
public:
    inline TestVector (){}
    TestVector cross (TestVector const& other) const;
};

template < typename Type >
inline TestVector< 3, Type > TestVector< 3, Type >::cross (TestVector< 3, Type > const& other) const
{
    return TestVector< 3, Type >();
}

void test ()
{
    TestVector< 3, double > vec0;
    TestVector< 3, double > vec1;
    vec0.cross(vec1);
}

在尝试编译这个简单示例时,我收到一个编译错误,指出“交叉”特化与现有声明不匹配:

error C2244: 'TestVector<Size,Type>::cross' : unable to match function definition to an existing declaration
see declaration of 'TestVector<Size,Type>::cross'
definition
    'TestVector<3,Type> TestVector<3,Type>::cross(const TestVector<3,Type> &) const'
    existing declarations
    'TestVector<Size,Type> TestVector<Size,Type>::cross(const TestVector<Size,Type> &) const'

我尝试将 cross 声明为模板:

template <size_t Size, typename Type>
class TestVector
{
public:
    inline TestVector (){}

    template < class OtherVec >
    TestVector cross (OtherVec const& other) const;
};

template < typename Type >
TestVector< 3, Type > TestVector< 3, Type >::cross< TestVector< 3, Type > > (TestVector< 3, Type > const& other) const
{
    return TestVector< 3, Type >();
}

此版本通过编译但在链接时失败:

 unresolved external symbol "public: class TestVector<3,double> __thiscall TestVector<3,double>::cross<class TestVector<3,double> >(class TestVector<3,double> const &)const

我在这里缺少什么? 谢谢, 弗洛伦特

【问题讨论】:

  • 一个问题是你没有部分特化class,而是其中的一些功能,你的class这里是模板化,所以是部分特化应该是另一个类模板。
  • @TonyTheLion 谢谢,所以你说我应该专门研究完整的 Vector 类?我的问题是它需要我重写整个类(它包含很多方法,而不仅仅是交叉)
  • 已提出问题。看到这个答案:stackoverflow.com/questions/13444615/…

标签: c++ templates specialization partial-specialization


【解决方案1】:

一种方法是将cross 定义为“函子”(即具有operator() 的类)。

template<size_t S, typename T>
class Vec {
  // ... stuff
  friend struct Cross<S, T>;
  Vec<S, T> cross(const Vec<S, T>& other) {
    return Cross<S, T>()(*this, other);
  }
  // ... more stuff
};


template<size_t S, typename T> struct Cross {
  Vec<S, T> operator() (const Vec<S, T>& a, const Vec<S, T>& b) {
    // general definition
  }
};

// Partial specialization
template<typename T> struct Cross<3, T> {
  vec<3, T> operator() (const Vec<3, T>& a, const Vec<3, T>& b) {
    // specialize definition
  }
};

【讨论】:

  • 谢谢,这是我的选择。但是提供的代码有一个小错误,操作符()有两个参数;)
  • @F.L.哎呀,对不起。固定的。谢谢。
【解决方案2】:

你不能部分特化一个方法。您可以在某些条件下超载。在这里,您可以选择您的课程的部分专业化

template <size_t Size, typename Type> class TestVector
{
public:
    inline TestVector (){}
    TestVector cross (TestVector const& other) const;
};

带有一般行为的定义:

TestVector<size_t Size, typename Type>::cross (TestVector const& other) const {
     // general
}

还有一个专门的模板,可以让您为案例 int 定义特定行为 3

template <typename Type> class TestVector<3, Type>
{
public:
    inline TestVector (){}
    TestVector cross (TestVector const& other) const;
};

带有自定义行为的定义:

TestVector<typename Type>::cross (TestVector const& other) const {
     // custom
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多