Modern C++字面量一网打尽

C++ Literals

数值

二进制、八进制、十六进制字面量

  • 二进制:0b0B开头。
  • 八进制:0开头。
  • 十六进制:0x0X开头。
// for different base
constexpr int base2 = 0b1111;  // 二进制 15
constexpr int base8 = 017;     // 8进制 15
constexpr int base10 = 15;     // 十进制 15
constexpr int base16 = 0xf;    // 16进制 15
// 检查
static_assert(base2 == 15);
static_assert(base8 == 15);
static_assert(base10 == 15);
static_assert(base16 == 15);

单引号作为数值分隔符

C++14 引入了单引号作为数值分隔符,可以提高可读性。

// ' separator for digit grouping
constexpr int a = 1'000'000;  // 1000000, 十进制
static_assert(a == 1000000);

// 同样适用于其他进制单位
constexpr int c = 0b1'0000;  // 16, 二进制
static_assert(c == 16);

constexpr int d = 01'0000;  // 4096, 八进制
static_assert(d == 4096);

constexpr int b = 0x1'0000;  // 65536, 十六进制
static_assert(b == 65536);

修饰存储类型

  • uU:表示无符号整数(unsigned)。
  • lL:表示长整型(long)。
  • llLL:表示长长整型(long long)。
  • z:表示带符号的std::size_t
// 修饰存储类型
#include <type_traits>
auto si = 1;  // int, 默认
static_assert(std::is_same_v<decltype(si), int>);

auto ui = 1u;  // unsigned int
static_assert(std::is_same_v<decltype(ui), unsigned int>);

auto ul = 1ul;  // unsigned long
static_assert(std::is_same_v<decltype(ul), unsigned long>);

auto ull = 1ull;  // unsigned long long
static_assert(std::is_same_v<decltype(ull), unsigned long long>);

auto l = 0l;  // long
static_assert(std::is_same_v<decltype(l), long>);

auto ll = 0ll;  // long long
static_assert(std::is_same_v<decltype(ll), long long>);

auto z = 0z;  // from C++23, 带符号的 std::size_t
static_assert(std::is_same_v<decltype(z), std::ptrdiff_t>);

auto uz = 0uz;  // from C++23
static_assert(std::is_same_v<decltype(uz), std::size_t>);
#include <vector>
void sample(std::vector<int>& vec) {
  // uz 的另外一个用法, 如果不加后缀会出现无符号数和有符号数的比较警告
  for (auto i = 0uz; i < vec.size(); ++i) {
    // do something
  }
  // 逆序遍历
  for (auto i = vec.size(); i--;) {
    // do something
  }
}

浮点数字面量修饰符:

  • fF:表示float类型。
  • lL:表示long double类型。
// 修饰浮点数
auto f = 1.0f;  // float
static_assert(std::is_same_v<decltype(f), float>);
auto d = 1.0;  // double, 默认
static_assert(std::is_same_v<decltype(d), double>);
auto ld = 1.0l;  // long double
static_assert(std::is_same_v<decltype(ld), long double>);

科学计数法

// 科学计数法
constexpr double e = 1e3;  // 1000.0
static_assert(e == 1000.0);
constexpr auto E = 1E3;  // 1000.0
static_assert(E == 1000.0);

字符字面量

字符字面量修饰符:

  • u:表示一个UTF-16字符(char16_t类型)。
  • U:表示一个UTF-32字符(char32_t类型)。
  • L:表示一个宽字符(wchar_t类型)。
  • u8:表示一个UTF-8字符(char类型)。
// 修饰字符
auto c = 'a';  // char, 默认
static_assert(std::is_same_v<decltype(c), char>);

auto wc = L'汉';  // wchar_t
static_assert(std::is_same_v<decltype(wc), wchar_t>);

auto u8c = u8'a';  // char8_t
static_assert(std::is_same_v<decltype(u8c), char8_t>);

auto u = u'a';  // char16_t
static_assert(std::is_same_v<decltype(u), char16_t>);

auto U = U'a';  // char32_t
static_assert(std::is_same_v<decltype(U), char32_t>);

字符串字面量

原始字符串字面量

对于多行字符串,可以使用原始字符串字面量,这样就不需要转义字符了。

// 原始字符串字面量
auto json = R"({"key": "value"})";
static_assert(std::is_same_v<decltype(json), const char*>);

std::string 字面量

C++14 引入了std::string字面量,可以直接使用字符串字面量初始化std::string对象。

// std::string 字面量
#include <string>
using namespace std::string_literals;  // C++14
auto str = "hello"s;                   // 注意这里的 s
static_assert(std::is_same_v<decltype(str), std::string>);

utf-8 字符串字面量

C++17 引入了utf-8字符串字面量,可以直接使用utf-8字符串字面量初始化std::string对象。

// utf-8 字符串字面量
auto utf8 = u8"你好";
static_assert(std::is_same_v<decltype(utf8), const char*>);

其他字面量

布尔字面量

// 布尔字面量
auto boolean = true;  // bool, 默认
static_assert(std::is_same_v<decltype(boolean), bool>);
auto b2 = false;  // bool
static_assert(std::is_same_v<decltype(b2), bool>);

空指针字面量

// 空指针字面量
auto null = nullptr;  // std::nullptr_t, 默认
static_assert(std::is_same_v<decltype(null), std::nullptr_t>);

用户定义的字面量(User-Defined Literals, UDL)

C++11 引入了用户定义的字面量(User-Defined Literals, UDL),允许程序员为自定义类型定义自己的字面量表示。这通过定义一个字面量运算符实现,运算符以 _ 开头,后跟一个或多个字符。

以下是一个简单的例子,定义一个用于表示长度的用户定义字面量:

#include <iostream>

// 定义一个表示长度的结构体
struct Length {
  double value; // 长度值
  enum Unit { meter, kilometer, centimeter }; // 单位
  Unit unit; // 长度单位

  // 构造函数
  Length(double val, Unit u) : value(val), unit(u) {}
};

// 用户定义的字面量,用于米
Length operator"" _m(long double val) {
  return Length(val, Length::meter);
}

// 用户定义的字面量,用于千米
Length operator"" _km(long double val) {
  return Length(val, Length::kilometer);
}

// 用户定义的字面量,用于厘米
Length operator"" _cm(long double val) {
  return Length(val, Length::centimeter);
}

int main() {
  Length length1 = 10.0_m; // 10米
  Length length2 = 5.0_km; // 5千米
  Length length3 = 100.0_cm; // 100厘米

  std::cout << "Length1: " << length1.value << " meters\n";
  std::cout << "Length2: " << length2.value << " kilometers\n";
  std::cout << "Length3: " << length3.value << " centimeters\n";

  return 0;
}

源码

完整的样例在这里.

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值