【发布时间】:2019-02-07 17:42:45
【问题描述】:
我想统一一个接口来处理模板化和非模板化类型。有没有办法确定类型(例如类或函数指针)是否依赖于模板参数?
例如:
struct foo {};
template<typename T> struct bar {};
// This works if the template parameter is provided
template<typename> struct is_templated : false_type {};
template<template<typename...> class Obj, typename...Args>
struct is_templated<Obj<Args...>> : true_type {};
template<typename T> constexpr auto is_templated_v = is_templated<T>::value;
在这种情况下,is_template_v<foo> 为假,is_template_v<bar<int>> 为真,但我不能只用is_template_v<bar> 推断任何东西。或者,如果我定义
template<template<typename...> class>
struct temp_check : true_type {};
那么temp_check<bar> 完全有效,但我不知道如何类似地检查foo。如果它是有效的 C++,则需要这样的东西
template<template<> class A> struct temp_check<A> : false_type {};
是否有某种机制可以同时检查两者?
【问题讨论】:
-
bar<int>是一种特定类型。另一方面,bar是一个模板。您可以同时检查两者,但您需要is_template的两个不同专业化才能这样做。