C++11 prohibits the use of a certain template method, combined with std::enable_if to achieve

scenes to be used:

There are multiple constructors, and at the same time a variable parameter constructor is implemented with a template. Now I want to call different constructors separately, but if the variable parameter constructor is not conditional, all initializations will call this variable parameter constructor. , This is caused by the principle of automatic template matching. Therefore, in some cases, I want to prohibit this variable parameter constructor. How to implement it with other constructors? Now the template combined with std::enable_if can achieve this requirement.

The role of std::enable_if can be found on the Internet. The following is a method for c++11 to prohibit the use of a certain template. This example can mainly be implemented for variable parameter templates.

Code:

#include <type_traits>
#include <iostream>
#include <memory>

class B {
public:
    B() { //没有参数的构造函数
        std::cout << "B init 0 param" << std::endl;
    }

    B(std::string ptr) { //1个参数的构造函数
        std::cout << "B init 1 param" << std::endl;
    };

    B(std::string sharedPtr, std::string sharedPtr1) { //2个参数的构造函数
        std::cout << "B init 2 param" << std::endl;
    }

    //3个参数的构造函数
    B(std::string sharedPtr, std::string sharedPtr1, std::string sharedPtr2) {
        std::cout << "B init 3 param string " << std::endl;
    }

    //可变参数的构造函数,只有参数大于3个才用此构造函数,假如不用std::enable_if加条件,则所有初始化都会调用此构造函数,模板匹配原则
    template<class... Args, typename std::enable_if<(sizeof...(Args) > 3)>::type...>
    B(Args&&... args) {
        fun1(std::forward<Args>(args)...);
        std::cout << "B init n parm" << std::endl;
    }

    ~B() {
        std::cout << "~B" << std::endl;
    };

    template<class U, class T>
    void fun1(U a, U b, U c, T d) {
        std::cout << "----fun1 template a:" << a << ",d:" << d << std::endl;
        funceb<T>(d);
    }
    
    template <typename F>
    typename std::enable_if<(sizeof(F) > 4)>::type funceb(F a)
    {
        std::cout << "----2222 funceb template a:" << a << std::endl;
    }
 

public:
    std::string a;
};

class D;
class C {
public:
    C(){
        std::cout << "0 C init" << std::endl;
    };

    //可变参数的类型都不一样的实现方式
    template<class... Args>
    void func(Args&&... args) {
        d_ptr = std::make_shared<D>(std::forward<Args>(args)...);
    }

    ~C() {};

private:
    std::shared_ptr<D> d_ptr;
};

class D {
public:
    D(){
        std::cout << "0 D init" << std::endl;
    };

    D(int a, std::string s, float f) {
        std::cout << "1 D init s:" << s << std::endl;
    };
    ~D() {};
};

int main()
{
    B b0;//用没有参数的构造函数
    B b("a1" ,"a2");//用只有两个参数的构造函数
    B b1(100 , 200 , 200, "hello"); //用可变参数的构造函数

    C c;
    c.func(1, "aaaaddddd", 2.0);//可变参数的多种数据类型的方式
}

 

Guess you like

Origin blog.csdn.net/hyl999/article/details/112661285