我想统一的接口与两个模板和非模板类型的工作。有没有一种方法来确定是否一个类型,如类或函数指针是依赖于一个模板参数?
例如:
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 {};
有一些机制,可以检查两者兼而有之?
我会使用的过载台的功率:
#include <iostream>
#include <type_traits>
struct foo {};
template<typename T> struct bar {};
template<template<class ...> class T, class... TArgs>
constexpr bool is_template() { return true; }
template<class T>
constexpr bool is_template() { return false; }
int main()
{
std::cout << is_template<foo>() << '\n'; // 0
std::cout << is_template<bar>() << '\n'; // 1
}
让用户:使用模板功能提供了特质;)