c++ - 如何使用 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, 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;
}

我要归档的是以下内容。它希望第一个 Ctor 仅在给出 NullType 时可用。在所有其他情况下,我想禁用第一个 Ctor 并启用第二个。

此时我收到一个编译错误,因为其中一个构造函数无效。但是我怎样才能使 Ctor 成为模板化的 Ctor 并重用类模板参数呢?

最佳答案

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

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);
}

EDIT1:或者假设您选择的编译器和您使用的 STL 实现支持 static_assert 和类型特征(即 VS 2010 支持),如果有人尝试使用禁用的 ctor,您可以获得更好的错误消息:

template<typename T, typename U = NullType>
struct TemplateStruct
{
    TemplateStruct(int i)
    {
        static_assert( std::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( !std::is_same<U,NullType>::value, "Cannot use two parameter ctor if U is not NullType!" );
        std::cout << "Two Param == " << i << "," << j << std::endl;
    }
};

EDIT2:或者如果您的 STL 中没有 is_same 但您有 static_assert:

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;
    }
};

关于c++ - 如何使用 mpl 技术启用构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5469805/

相关文章:

c++ - 如何授予函数模板好友访问类的权限?

c++ - 作为对象成员或模板参数的不同行为

c++ - 错误 MSB6006 : "cmd.exe" exited with code

c++ - 多次在 Boost 测试中重复测试用例

wpf - 设置 ComboboxItem 的 MaxWidth 以使项目宽度不超过 ComboboxWidth

c++ - Numpy 的 __array_interface__ 不返回字典

c++ - 由于在 aix6.1 上使用 g++4.6.1 出现 "The displacement must be greater than or equal to -32768 and less than or equal to 32767",编译失败

c++ - 我收到错误 : call of overloaded ‘max(Cents&, Cents&)’ is ambiguous

c++ - 分配动态分配的指针数组

c++ - 如何在恢复提交和保留单元测试的同时检查测试?