C++检测类型是否具有模板参数
C++ detect if type has template parameter
我想统一一个界面,以便同时使用模板化和非模板化类型。有没有办法确定类型(如类或函数指针(是否依赖于模板参数?
例如:
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
}
让用户:使用模板功能提供特征;)
相关文章:
- 如何检测参数包中的uint8_t类型
- 无法使用 auto 来参数化true_type来检测 T::value()
- 检测习惯用语和默认参数类型匹配
- 检测传递给函数模板函数参数的特定函数
- 类型检测:使用variadic参数正确实现计算平均值的函数
- 如何在 Lua 回调中检测可选的第一个参数
- 有没有办法在编译时检测是否可以使用一组给定的参数类型成功调用通用 lambda?
- C++默认参数通过_CrtMemDifference屏蔽内存泄漏检测
- LLVM 的位码错误检测函数参数
- 参数类型的编译时类型检测
- 检测一个可以使用模板元编程可以采用的函数的最大参数数量
- 如何检测我何时向可变参数函数传递"std::string"而不是"c_str()"
- C++检测类型是否具有模板参数
- 如何在变量函数参数列表中检测给定类型的参数的可用性,并在处理完所有参数后采取行动
- 提升程序选项:参数被多次检测到
- 错误C2664:无法转换参数(指纹传感器检测)
- 函数有没有办法自动检测其参数的数据类型?(请不要建议函数重载)
- 如何检测作为参数传递的函数的参数类型
- SFINAE -用一个参数检测构造函数
- 用显式参数检测自由函数的存在性