C++ 是否有任何类型特征控制成员类型(不是成员变量)
STL具有许多类型特征,如C++ 是否有任何类型特征控制成员类型(不是成员变量),c++,c++11,typetraits,C++,C++11,Typetraits,STL具有许多类型特征,如std::is_指针,std::is_引用等 假设我有一节课 class A { using type_member = void; } 是否有任何类型特征用于控制类型成员并检查其是否存在? 类似于的东西是_type_member_exist() 我既好奇C++17是否有解决方案,也好奇C++2003(在工作中我需要它,我有vs2010,它有一点C++11支持,但不完整)。如果type_member是public(而不是你所问的private),我想
std::is_指针
,std::is_引用
等
假设我有一节课
class A
{
using type_member = void;
}
是否有任何类型特征用于控制类型成员并检查其是否存在?类似于
的东西是_type_member_exist()代码>
我既好奇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
}
#包括
模板
具有\u类型\u成员的结构
{
模板
静态constexpr bool getValue(int,typename Y::type_member*=nullptr)
{返回true;}
静态constexpr bool getValue(长)
{返回false;}
静态constexpr布尔值{getValue(0)};
};
甲级
{
公众:
使用类型_成员=无效;
};
int main()
{
std::cout如果type_成员
是公共的
(而不是你问题中的私有的
),我想你可以这样做
#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
}
#包括
模板
具有\u类型\u成员的结构
{
模板
静态constexpr bool getValue(int,typename Y::type_member*=nullptr)
{返回true;}
静态constexpr bool getValue(长)
{返回false;}
静态constexpr布尔值{getValue(0)};
};
甲级
{
公众:
使用类型_成员=无效;
};
int main()
{
我相信cout成员变量和成员类型是不同的。成员类型可能不同,因为它们是纯类型。语言可能会为它们提供一个更简单的解决方案。你提出的问题对我没有帮助。查找void\u t
。看。伙计们,你可以用一个例子来写一个答案吗解释为什么@MooingDuck链接中的示例无法回答您的问题(并编辑以显示差异)?我相信成员变量和成员类型是不同的。成员类型可能会有所不同,因为它们是纯类型。语言可能会为它们提供更简单的解决方案。你提出的问题对我没有帮助。查找void\t
。看。伙计们,你能用一个示例来写一个答案吗?你能解释一下吗为什么@MooingDuck链接中的示例无法回答您的问题(并编辑以显示差异)?