Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.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++ 如何使用mpl技术启用构造函数_C++_Templates_Boost_Boost Mpl - Fatal编程技术网

C++ 如何使用mpl技术启用构造函数

C++ 如何使用mpl技术启用构造函数,c++,templates,boost,boost-mpl,C++,Templates,Boost,Boost Mpl,我对boost::enable_if以及如何使用它进行构造函数切换有点拘泥 代码如下: struct NullType{}; struct TestType{}; struct NonNull{}; template<typename T, typename U = NullType> struct TemplateStruct { TemplateStruct(int i, typename boost::enable_if<boost::is_same<U,

我对boost::enable_if以及如何使用它进行构造函数切换有点拘泥

代码如下:

struct NullType{};
struct TestType{};
struct NonNull{};

template<typename T, typename U = NullType>
struct TemplateStruct
{
    TemplateStruct(int i, typename boost::enable_if<boost::is_same<U, NullType>, void* >::type dummy = 0)
    {
        std::cout << "One Param == " << i << std::endl;
    }

    TemplateStruct(int i, int j, typename boost::disable_if<boost::is_same<U, NullType>, void* >::type dummy = 0)
    {
        std::cout << "Two Param == " << i << "," << j << std::endl;
    }
};

int main(int /*argc*/, char**)
{
    TemplateStruct<TestType>(1);
    TemplateStruct<TestType,NonNull>(1,2);
    return 0;
}
struct NullType{};
结构测试类型{};
结构非空{};
模板
结构模板结构
{
TemplateStruct(int i,typename boost::enable_if::type dummy=0)
{

我不知道你的确切问题的解决办法

也许这段代码会有所帮助(请注意,您不需要为此启用_)。至少它可以作为更好解决方案的起点:

#include <iostream>

struct NullType{};
struct TestType{};
struct NonNull{};

template<typename T, typename U>
struct TemplateStruct
{  
    TemplateStruct(int i, int j)
    {
        std::cout << "Two Param == " << i << "," << j << std::endl;
    }
};

template<typename T>
struct TemplateStruct<T, NullType>
{
    TemplateStruct(int i)
    {
        std::cout << "One Param == " << i << std::endl;
    }
};

int main(int /*argc*/, char**)
{
    TemplateStruct<TestType,NullType>(1);
    TemplateStruct<TestType,NonNull>(1,2);
    return 0;
}
#包括
结构空类型{};
结构测试类型{};
结构非空{};
模板
结构模板结构
{  
TemplateStruct(inti,intj)
{

std::cout这是一种根据需要解决问题的方法:

template<typename T, typename U = NullType>
struct TemplateStruct
{
    TemplateStruct(int i)
    {
        boost::enable_if< boost::is_same<U,NullType>, void*>::type var = nullptr;
        std::cout << "One Param == " << i << std::endl;
    }

    TemplateStruct(int i, int j)
    {
        boost::disable_if< boost::is_same<U,NullType>, void*>::type var = nullptr;
        std::cout << "Two Param == " << i << "," << j << std::endl;
    }
};

int main()
{
    TemplateStruct<TestType>(1);
    TemplateStruct<TestType,NonNull>(1,2);
    //will not compile TemplateStruct<TestType>(1,2);
    //will not compile TemplateStruct<TestType,NonNull>(1);
}
模板
结构模板结构
{
模板结构(int i)
{
boost::enable_如果::type var=nullptr;

解决这个问题的一种方法是在实际类周围有一个小包装器,并专门化这个包装器。如果您不想启用/禁用两个函数,那么这个方法很有效

struct NullType{};
struct TestType{};
struct NonNull{};

template<typename T, typename U>
struct TemplateStruct
{
    TemplateStruct(int i){
        std::cout << "One Param == " << i << std::endl;
    }

    TemplateStruct(int i, int j){
        std::cout << "Two Param == " << i << "," << j << std::endl;
    }

    void FunctionToDisableFor1Param(){
    }

    void FunctionToAlwaysEnable(){
    }
};

template<class T, class U = NullType>
struct TSWrapper : public TemplateStruct<T,U>{
    typedef TemplateStruct<T,U> BaseType;

    TSWrapper(int i, int j)
        : BaseType(i,j)
    {}
};

template<class T>
struct TSWrapper<T,NullType> : public TemplateStruct<T,NullType>{
    typedef TemplateStruct<T,NullType> BaseType;

    TSWrapper(int i)
        : BaseType(i)
    {}

private:
    using BaseType::FunctionToDisableFor1Param;
};

int main()
{
    TSWrapper<TestType> x(1);
    TSWrapper<TestType,NonNull> y(1,2);
    x.FunctionToAlwaysEnable();
    y.FunctionToDisableFor1Param();
    // uncomment for compile time error
    //x.FunctionToDisableFor1Param();
    return 0;
}
struct NullType{};
结构测试类型{};
结构非空{};
模板
结构模板结构
{
模板结构(int i){

std::您的任务可以启用/禁用类专门化而不是使用构造函数吗?我想这是唯一的方法。但是我担心类专门化会导致代码膨胀。@mkaes:这里通常的技术是将所有常见的东西都考虑到(模板)中基类。@Alexandre C.:…然后您必须在该基类中声明构造函数--回到最初的问题。@Alexander:基类中的构造函数不是要被禁用/启用的(并且应该受到
保护
)因为它们对所有最终用户类都是通用的。在派生类中,您对
NullType
有一个专门化,并且不再需要SFINAE。这将是一个解决方案,但模板类非常大,并且旧代码不是我提供的。因此,要使用您的解决方案,我必须找到一种方法,将所有方法从类提取到类共享类,所以我不必复制它们。这是我希望避免的事情。
template<typename T, typename U = NullType>
struct TemplateStruct
{
    TemplateStruct(int i)
    {
        static_assert( boost::is_same<U,NullType>::value, "Cannot use one parameter ctor if U is NullType!" );
        std::cout << "One Param == " << i << std::endl;
    }

    TemplateStruct(int i, int j)
    {
        static_assert( !boost::is_same<U,NullType>::value, "Cannot use two parameter ctor if U is not NullType!" );
        std::cout << "Two Param == " << i << "," << j << std::endl;
    }
};
struct NullType{};
struct TestType{};
struct NonNull{};

template<typename T, typename U>
struct TemplateStruct
{
    TemplateStruct(int i){
        std::cout << "One Param == " << i << std::endl;
    }

    TemplateStruct(int i, int j){
        std::cout << "Two Param == " << i << "," << j << std::endl;
    }

    void FunctionToDisableFor1Param(){
    }

    void FunctionToAlwaysEnable(){
    }
};

template<class T, class U = NullType>
struct TSWrapper : public TemplateStruct<T,U>{
    typedef TemplateStruct<T,U> BaseType;

    TSWrapper(int i, int j)
        : BaseType(i,j)
    {}
};

template<class T>
struct TSWrapper<T,NullType> : public TemplateStruct<T,NullType>{
    typedef TemplateStruct<T,NullType> BaseType;

    TSWrapper(int i)
        : BaseType(i)
    {}

private:
    using BaseType::FunctionToDisableFor1Param;
};

int main()
{
    TSWrapper<TestType> x(1);
    TSWrapper<TestType,NonNull> y(1,2);
    x.FunctionToAlwaysEnable();
    y.FunctionToDisableFor1Param();
    // uncomment for compile time error
    //x.FunctionToDisableFor1Param();
    return 0;
}