C++ std::is_same示例

std::is_same,继承自 integral_constant,用于判断T和U是否为相同的类型,返回值(::value)为true/false。

template <class T, class U> struct is_same;

注意,若class类名不同,当且仅当T与U有typedef关联,否则就算T与U的所有成员属性完全一致,也仍认为是不同的类型。
具体示例为:

// is_same example
#include <iostream>
#include <type_traits>
#include <cstdint>

typedef int integer_type;
struct A { int x,y; };
struct B { int x,y; };
typedef A C;

int main() {
  std::cout << std::boolalpha; //把bool值显示为true或false。
  std::cout << "is_same:" << std::endl;
  std::cout << "int, const int: " << std::is_same<int, const int>::value << std::endl;
  std::cout << "int, integer_type: " << std::is_same<int, integer_type>::value << std::endl;
  std::cout << "A, B: " << std::is_same<A,B>::value << std::endl;
  std::cout << "A, C: " << std::is_same<A,C>::value << std::endl;
  std::cout << "signed char, std::int8_t: " << std::is_same<signed char,std::int8_t>::value << std::endl;
  return 0;
}

输出为:

is_same:
int, const int: false
int, integer_type: true
A, B: false
A, C: true
signed char, std::int8_t: true
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值