类定义中的模板专门化 [英] Template specialization within class definition

查看:44
本文介绍了类定义中的模板专门化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能将此类的整个代码放入类中(类似于Java).我这样做是为了编写一段代码,而不是必须搜索每个函数,而是希望将整个类放在一张纸上(是的,我确实打印过,现在我倾向于喜欢纸)

I wonder whether it is possible to put the whole code of such a class inside the class (kind of as in Java). I'm doing this for some piece of code, instead of having to search for each function, I'd rather have the whole class on a single sheet of paper (yes, I do print them, I tend to like paper these days).

#include <iostream>

template <class V> class A {
public:
    A();
};

template <class V> A<V>::A() {
    std::cout<<"Generic"<<std::endl;
}

template <> A<bool>::A() {
    std::cout<<"bool"<<std::endl;
}

int main(int argc, char** argv) {
    A<int> a;
    A<bool> b;
}

现在有可能沿这些路线获得一些东西吗?

Now is it possible to get something along those lines ?

#include <iostream>

template <class V> class A {
public:
    A() {
        std::cout<<"Generic"<<std::endl;
    };
    /* somethig specifying specialization for bool */ A() {
        std::cout<<"bool"<<std::endl;
    }
};

int main(int argc, char** argv) {
    A<int> a;
    A<bool> b;
}

这有可能吗?

推荐答案

是的,可以使用 std :: enable_if 选择合适的构造函数,而无需专门化即可在单个类定义中包含所有内容像这样:

Yes, it is possible to have everything in a single class definition without specializations, using std::enable_if to choose the appropriate constructor like this:

template <bool C, typename T = void>
using only_if = typename std::enable_if <C, T>::type;

template <typename A, typename B>
using eq = typename std::is_same <A, B>::type;

template <class V> class A {
public:
    template <typename D = int, only_if <!eq <V, bool>{}, D> = 0>
    A() { std::cout<<"Generic"<<std::endl; };

    template <typename D = int, only_if <eq <V, bool>{}, D> = 0>
    A() { std::cout<<"bool"<<std::endl; }
};

其中模板别名 only_if eq 只是为了简洁.

where template aliases only_if and eq are just for brevity.

模板参数 D 是虚拟的.通常,我们在模板参数,函数参数或返回类型上应用 enable_if .非模板默认构造函数是一个唯一的异常,没有上面的任何内容,因此是虚拟的.

Template parameter D is dummy. Usually we apply enable_if on a template parameter, a function argument, or a return type. A non-template default constructor is a unique exception having nothing of the above, hence the dummy.

对于这个简单的示例(模板专门化可能更简单)而言,这种方法可能是过大的选择.但是,只需要一个专门针对一个构造函数的30行代码类的类,肯定会比通过整个类的专业化复制所有代码的方式更加简单.可能有人争辩说,在这种情况下,可以使用仅包含需要专门化的内容的基类来重构代码.但是:

This approach is maybe an overkill for this simple example, where a template specialization may be simpler. But a class of 30 lines of code that needs a specialization like that for just one constructor will be definitely simpler this way rather than duplicating all code for the entire class specialization. One may argue that in this case the code may be refactored using a base class that contains only what needs to be specialized. However:

  • 在某些情况下,您不想在两个构造函数版本之间进行选择,而只是根据类型谓词来启用或禁用单个版本,例如类型是否为 std :: default_constructible .

或者,您可能需要再次根据类型谓词(因此,提供显式和非显式版本)来决定是否将构造函数声明为 explicit ./p>

Or, you may need to decide whether a constructor is declared explicit or not, again depending on a type predicate (so, provide an explicit and a non-explicit version).

在这种情况下, enable_if 非常方便.

In such cases, enable_if is very convenient.

在此处检查一个非常通用的元组实现的示例,其中包含五个构造函数,全部使用 enable_if ,一个构造函数(默认)使用虚拟模板参数.其余四个是显式与非显式以及元素列表与其他元组的组合.

Check here an example of a very generic tuple implementation with five constructors, all using enable_if, and one (the default) using a dummy template parameter. The remaining four are for the combinations of explicit vs. non-explicit and element-list vs other-tuple.

这篇关于类定义中的模板专门化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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