是否有任何类型特征控制成员类型(不是成员变量)
Is there any type trait which controls member type(not member variable)
STL具有许多类型特征,如std::is_pointer
,std::is_reference
等...
假设我有一个班级
class A
{
using type_member = void;
}
是否有任何类型特征用于控制类型成员并检查它是否存在?
类似is_type_member_exist<typename A::type_member>();
我既好奇 C++17 是否存在解决方案,也对 C++2003 感到好奇(在工作中我需要这个,我有 vs2010,它有一点 C++11 支持但不完整(。
如果type_member
是public
(而不是像你的问题中那样private
(,我想你可以做类似的事情
#include <iostream>
template <typename X>
struct with_type_member
{
template <typename Y = X>
static constexpr bool getValue (int, typename Y::type_member * = nullptr)
{ return true; }
static constexpr bool getValue (long)
{ return false; }
static constexpr bool value { getValue(0) };
};
class A
{
public:
using type_member = void;
};
int main ()
{
std::cout << with_type_member<int>::value << std::endl; // print 0
std::cout << with_type_member<A>::value << std::endl; // print 1
}
希望这有帮助。
相关文章:
- 访问C++中的类型成员
- 具有 STL 向量类型成员的类的复制内存
- 重载具有 2 个相同数据类型成员的构造函数
- std::void_t 和嵌套的非类型成员
- 返回对常量结构(指针类型)成员的引用:明显的左值到右值转换
- 使用各种数据类型成员创建对象的简便方法
- 将类类型成员定义为公共和私有之间有什么区别?
- 结构类型成员的默认构造函数中的默认参数
- 如何传递模板模板非类型成员函数指针
- 文本类类型成员函数约束
- 给定仅包含布尔类型成员的结构的两个对象 s1 和 s2,只要 s1 的成员为 true,请检查 s2 的每个成员是否为真
- 如何在 c++ 中使用二进制文件输入/输出读取/写入结构的字符串类型成员
- C++:将模板参数的模板类型成员加为好友的语法正确吗
- C++静态结构类型成员初始化
- C++:作为引用或指针的基类型成员变量
- 左值引用类型成员的用户定义移动构造函数
- 具有指针类型成员的类在 MSVS2012@debug 下销毁时失败.程序错误或错误源
- CRTP -- 访问不完整的类型成员
- 为什么C++默认初始化不对非类类型成员进行零初始化
- 请求非类类型成员