Skip to content

User literals(C++11) #140

@Wunkolo

Description

@Wunkolo

Channel

C++Weekly

Topics

What topics are important to cover for this episode?

User-literals and use-cases for user-literals. Such as memory-size user-literals.

// namespace My::Cool::Library::Literals {

constexpr std::size_t operator""_KiB(unsigned long long int x) {
  return 1024ULL * x;
}

constexpr std::size_t operator""_MiB(unsigned long long int x) {
  return 1024_KiB * x;
}

constexpr std::size_t operator""_GiB(unsigned long long int x) {
  return 1024_MiB * x;
}

constexpr std::size_t operator""_TiB(unsigned long long int x) {
  return 1024_GiB * x;
}

constexpr std::size_t operator""_PiB(unsigned long long int x) {
  return 1024_TiB * x;
}

// } // namespace My::Cool::Library::Literals
...
	using namespace My::Cool::Library::Literals;
...
	const std::size_t BufferSize = 8_MiB;
	...
	std::byte LogBuffer[64_KiB];
	...
	Config.StackSize = 16_MiB;
	...
	class Foo {
	public:
		Foo() : memory_size_(64_MiB) {
	...
	std::size_t new_size = round_up(old_size, 64_KiB);
	...
	if( Size >= 1_GiB )
	...
	BufferThing(std::size_t InitialSize = 1_MiB);
	...
	const uint8_t BufferSize = 4_GiB;
	// warning: implicit conversion from 'size_t' (aka 'unsigned long') to 'const uint8_t' (aka 'const unsigned char') changes value from 4294967296 to 0 [-Wconstant-conversion]
	// const uint8_t BufferSize = 4_GiB;
	//               ~~~~~~~~~~   ^~~~~

Length

5-10 minutes

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions