Partial Specialization Tasks

I am trying to write a generalized vector class of size and type for mathematical programming. I have problems with partial specialization.

The problem occurs when I try to specialize a member method of my vector class for a given size.

I can give a simple example:

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

When I try to compile this simple example, I get a compilation error indicating that the specialization "cross" does not match the existing declaration:

 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' 

I tried to declare a cross as a pattern:

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

This version passes the compilation, but does not work in link-time:

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

What am I missing here? Thanks, Florent

+4
source share
2 answers

One way to do this is to define cross as a β€œfunctor” (that is, a class with 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 } }; 
+1
source

You cannot partially specialize a method. You can overload under certain conditions. Here you can go with a partial specialization of your class

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

with a definition for general behavior:

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

and a specialized template that allows you to define specific behavior for the int case is 3

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

with a definition for user behavior:

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

Source: https://habr.com/ru/post/1447456/


All Articles