Partial specialization for one method in the class

倾然丶 夕夏残阳落幕 提交于 2021-02-11 12:23:23

问题


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

    // ...
}

回答1:


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

    // ...
};


来源:https://stackoverflow.com/questions/61795331/partial-specialization-for-one-method-in-the-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!