c++ constexpr 使用

#include <iostream>
#include <string>
#include <type_traits>

struct FinA{
  constexpr FinA(unsigned long long x, int y):x(x), y(y){}

  FinA& zero() {
    return *this;
  }

  friend std::ostream&operator << (std::ostream& stm, const FinA& a) {
    return  stm <<  "A{" << a.x << a.y << "}";
  }

 public:
  unsigned long long int x;
  int y;
};

constexpr  FinA operator "" _xy(unsigned long long xy) {
  return {xy/1000, int(xy% 1000)};
}

int main() {

  // FinA a(1,2);
  FinA  a {1,2};

  // 如果 operator "" _xy 不声明为constexp ,并且在构造的时候需要声明为constexpr
  // a2 声明为 constexpr 会报错
  constexpr FinA a2 = 12131_xy;


  std::cout << a2.x << std::endl;
  std::cout << a.y << std::endl;

  std::cout << std::boolalpha
            << "object type? " << std::is_object<decltype(a2)>::value << '\n'

            << "scalar type? " << std::is_scalar<decltype(a2)>::value << '\n'

            << "literal type? "
            << std::is_literal_type<decltype(a2)>::value << "\n\n" ;

  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝鲸123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值