Search code examples
c++templatespartialsspecialization

Partial specialization for one method in the class


I have a template matrix class. I try to implement the size (always square) as a template parameter.

template< 
    // Type of data
    typename type_t,
    // dimension of the matrix 4 -> 4x4 
    std::size_t dim_t, 
    // I don't want a matrix of non numeric value
    typename = typename std::enable_if_t< std::is_arithmetic_v< type_t >, type_t > 
>
class Matrix final
{
    // ....
}

With this code, I would like to be able to make a different method for different sizes of matrix. Because some method force me to take it account without going in to dark code...

// For instance
void doSomething (void);

Is there a way like the following to be used ? Because I saw a lot of example and I have always an error. I use C++17 and GCC. The final idea is to have only a header file.

template< 
    typename type_t,
    std::size_t dim_t, 
    typename = typename std::enable_if_t< std::is_arithmetic_v< type_t >, type_t > 
>
class Matrix final
{
    // ...

    // Specialization for matrix 4x4
    template < typename type_t >
    void doSomething< type_t, 4 > (void)
    {
         // Specific code for matrix 4x4.
    }

    // ...
}

Solution

  • You might use SFINAE:

    template< 
        typename type_t,
        std::size_t dim_t, 
        typename = typename std::enable_if_t< std::is_arithmetic_v< type_t >, type_t > 
    >
    class Matrix final
    {
        // ...
    
        // Specialization for matrix 4x4
        template <std::size_t N = dim_t, std::enable_if_t<N == 4, int> = 0>
        void doSomething()
        {
             // Specific code for matrix 4x4.
        }
    
        // ...
    };
    

    C++20 would allow requires for better syntax:

    template< 
        typename type_t,
        std::size_t dim_t, 
        typename = typename std::enable_if_t< std::is_arithmetic_v< type_t >, type_t > 
    >
    class Matrix final
    {
        // ...
    
        // Specialization for matrix 4x4
        void doSomething() requires(dim_t == 4)
        {
             // Specific code for matrix 4x4.
        }
    
        // ...
    };