在我的程序中,我有以下代码:
template<typename... Args>
class Outer {
template<typename Arg>
class Inner {
//some code
}
}
如何将Arg
限制为Args模板包中的某种类型?
答案 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");
};
};