如何有条件地向类模板添加函数? [英] How to conditionally add a function to a class template?

查看:124
本文介绍了如何有条件地向类模板添加函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Matrix类模板,如下所示:

I have a Matrix class template as follows:

template<typename T, std::size_t nrows, std::size_t ncols>
class Matrix
{
    T data[nrows][ncols];
public:
    T& operator ()(std::size_t i, std::size_t j)
    {
        return data[i][j];
    }
};

我要定义的是 .setIdentity()仅在编译时 nrows == ncols true 的实例化函数。当 nrows == ncols .setIdentity()时,将没有没有定义。 false

What I want is to define a .setIdentity() function only for instantiations when nrows==ncols is true at compile time. And there will be no definition of .setIdentity() when nrows==ncols is false.

我正在尝试使用 enable_if 习惯用法,但这将定义所有情况的功能。

What I am trying is using enable_if idiom, but that will define the function for all cases. Isn't it?

推荐答案

您可以使用 std :: enable_if 在以下模式下

You can do it with std::enable_if in the following mode

template <std::size_t r = nrows, std::size_t c = ncols>
typename std::enable_if<r == c>::type setIdentity ()
 { /* do something */ }

完整示例

#include <type_traits>


template<typename T, std::size_t nrows, std::size_t ncols>
class Matrix
{
    T data[nrows][ncols];

public:
    T& operator ()(std::size_t i, std::size_t j)
    { return data[i][j]; }

    template <std::size_t r = nrows, std::size_t c = ncols>
    typename std::enable_if<r == c>::type setIdentity ()
     { /* do something */ }
};

int main()
 {
   Matrix<int, 3, 3>  mi3;
   Matrix<int, 3, 2>  mnoi;

   mi3.setIdentity();
   // mnoi.setIdentity(); error

  return 0;
}

---编辑---

正如Niall在评论中指出的(关于TemplateRex的答案,但我的解决方案也存在相同的缺陷),这种解决方案可以绕开这种方式来显式地显示行数和列数

As pointed in a comment by Niall (regarding the TemplateRex's answer, but my solution suffer from the same defect) this solution can be circonvented expliciting the number of rows and columns in this way

mi3.setIdentity<4, 4>();

(但这不是真正的问题(IMHO),因为 mi3 是一个方矩阵, setIdentity()可以使用实际尺寸( nrows ncols ))甚至与

(but this isn't a real problem (IMHO) because mi3 is a square matrix and setIdentity() could work with real dimensions (nrows and ncols)) or even with

mnoi.setIdentity<4, 4>()

(这是个大问题(恕我直言),因为 mnoi 不是平方矩阵。

(and this is a big problem (IMHO) because mnoi isn't a square matrix).

很显然,Niall提出了解决方案(添加 static_assert ;类似

Obviously there is the solution proposed by Niall (add a static_assert; something like

    template <std::size_t r = nrows, std::size_t c = ncols>
    typename std::enable_if<r == c>::type setIdentity ()
     {
       static_assert(r == nrows && c == ncols, "no square matrix");

       /* do something else */ 
     }

或类似的内容),但我建议在 std :: enable_if 中添加相同的支票。

or something similar) but I propose to add the same check in std::enable_if.

我的意思是

template <std::size_t r = nrows, std::size_t c = ncols>
typename std::enable_if<    (r == c)
                         && (r == nrows)
                         && (c == ncols)>::type setIdentity ()
 { /* do something */ }

这篇关于如何有条件地向类模板添加函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆