How to conditionally add a function to a class template?

前端 未结 5 2044
你的背包
你的背包 2020-12-29 10:45

I have a Matrix class template as follows:

template
class Matrix
{
    T data[nrows][ncols];
public:
         


        
5条回答
  •  天命终不由人
    2020-12-29 11:30

    skypjack and max66 have both presented simple answers to the problem. This is just an alternate way of doing it, using simple inheritance, although it means the use of a child class for square matrices:

    template
    class Matrix
    {
    protected:
        T data[nrows][ncols];
    public:
        T& operator ()(std::size_t i, std::size_t j)
        {
            return data[i][j];
        }
    };
    
    template
    class SqMatrix : public Matrix 
    {
    public:
        setIdentity()
        {
            //Do whatever
        }
    }
    

提交回复
热议问题