检查C ++中两种类型是否相等 [英] Check if two types are equal in C++

查看:87
本文介绍了检查C ++中两种类型是否相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查C ++ 11中的类型是否相等?

How to check if types are equal in C++11?

 std::uint32_t == unsigned;  //#1

和另一个代码段

template<typename T> struct A{ 
  string s = T==unsigned ? "unsigned" : "other";
}


推荐答案

您可以使用 std :: is_same< T,U> :: value 从C ++ 11开始。

You can use std::is_same<T,U>::value from C++11 onwards.

此处, T U 是类型,而 value 将是 true (如果相等), false (如果不相同)。

Here, T, and U are the types, and value will be true if they are equivalent, and false if they are not.

请注意,这是在编译时评估的。

Note that this is evaluated at compile time.

请参见 http://en.cppreference.com/w/cpp/types/is_same

这篇关于检查C ++中两种类型是否相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆