Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 类定义中的模板专门化_C++_Templates - Fatal编程技术网

C++ 类定义中的模板专门化

C++ 类定义中的模板专门化,c++,templates,C++,Templates,我想知道是否有可能将这样一个类的全部代码放在类中(类似于Java)。我这样做是为了一些代码,而不是必须搜索每个函数,我更希望整个类都放在一张纸上(是的,我确实打印了它们,我现在喜欢纸) #包括 模板类别A{ 公众: A(); }; 模板A::A(){ std::cout是 这是完全可能的,尽管专门化必须在其他模板中完成 #include <iostream> template <class V> class A { public: A() { s

我想知道是否有可能将这样一个类的全部代码放在类中(类似于Java)。我这样做是为了一些代码,而不是必须搜索每个函数,我更希望整个类都放在一张纸上(是的,我确实打印了它们,我现在喜欢纸)

#包括
模板类别A{
公众:
A();
};
模板A::A(){
std::cout是

这是完全可能的,尽管专门化必须在其他模板中完成

#include <iostream>

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

template <> class A<bool>
{
public:
    A() { std::cout << "Bool specialization" << endl; }
};

int main(int argc, char** argv) {
    A<int> a;
    A<bool> b;
}
#包括
模板类别A{
公众:
(){

std::cout是的,可以将所有内容都放在一个类定义中,而无需专门化,使用
std::enable\u if
选择合适的构造函数,如下所示:

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; }
};
模板
仅使用_if=typename std::enable_if::type;
模板

使用eq=typename std::是一个非常通用的元组实现示例,它有五个构造函数,所有构造函数都使用
enable\u if
,一个(默认值)使用伪模板参数。其余四个用于显式与非显式以及元素列表与其他元组的组合。

啊,是的,这仍然是几个“单位”,但这确实比我最初的想法要好。从技术上讲,这是最正确的答案,尽管代码可读性为负1:)
#include <iostream>

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

template <> class A<bool>
{
public:
    A() { std::cout << "Bool specialization" << endl; }
};

int main(int argc, char** argv) {
    A<int> a;
    A<bool> b;
}
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; }
};