如何仅为父类可变参数模板参数启用嵌套类模板?

时间:2015-12-02 05:10:08

标签: c++ templates c++11

在我的程序中,我有以下代码:

template<typename... Args>
class Outer {
    template<typename Arg>
    class Inner {
        //some code
    }
}

如何将Arg限制为Args模板包中的某种类型?

1 个答案:

答案 0 :(得分:1)

#include <type_traits>

template <typename T, typename... Ts>
struct contains : std::false_type {};

template <typename T, typename F, typename... Ts>
struct contains<T, F, Ts...> : contains<T, Ts...> {};

template <typename T, typename... Ts>
struct contains<T, T, Ts...> : std::true_type {};

template <typename... Args>
struct Outer
{
    template <typename Arg>
    struct Inner
    {
        static_assert(contains<Arg, Args...>::value, "Arg must be from Args");
    };
};

DEMO