Skip to content

Instantly share code, notes, and snippets.

@Robbepop
Created June 17, 2015 17:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Robbepop/745c784311cf9ee71fc2 to your computer and use it in GitHub Desktop.
Save Robbepop/745c784311cf9ee71fc2 to your computer and use it in GitHub Desktop.
Some helper user defined literals in order to improve syntax with an extensive use of uniform initialization.
#include <cstdint>
#include <cstddef>
namespace explicit_types {
//====================================================================================
// Operator definitions for signed int types
//====================================================================================
constexpr auto operator"" _i8 (unsigned long long value) noexcept -> int8_t { return value; }
constexpr auto operator"" _i16(unsigned long long value) noexcept -> int16_t { return value; }
constexpr auto operator"" _i32(unsigned long long value) noexcept -> int32_t { return value; }
constexpr auto operator"" _i64(unsigned long long value) noexcept -> int64_t { return value; }
//====================================================================================
// Operator definitions for unsigned int types
//====================================================================================
constexpr auto operator"" _u8 (unsigned long long value) noexcept -> uint8_t { return value; }
constexpr auto operator"" _u16(unsigned long long value) noexcept -> uint16_t { return value; }
constexpr auto operator"" _u32(unsigned long long value) noexcept -> uint32_t { return value; }
constexpr auto operator"" _u64(unsigned long long value) noexcept -> uint64_t { return value; }
//====================================================================================
// Operator definitions for floating point types
//====================================================================================
constexpr auto operator"" _f32(unsigned long long value) noexcept -> float { return value; }
constexpr auto operator"" _f64(unsigned long long value) noexcept -> double { return value; }
constexpr auto operator"" _f32(long double value) noexcept -> float { return value; }
constexpr auto operator"" _f64(long double value) noexcept -> double { return value; }
//====================================================================================
// Operator definitions for special built in types
//====================================================================================
constexpr auto operator"" _size(unsigned long long value) noexcept -> size_t { return value; }
}
using namespace explicit_types;
void test_signed() {
auto a = 123_i8;
auto b = 123_i16;
auto c = 123_i32;
auto d = 123_i64;
static_assert(sizeof(a) == sizeof(int8_t) , "");
static_assert(sizeof(b) == sizeof(int16_t), "");
static_assert(sizeof(c) == sizeof(int32_t), "");
static_assert(sizeof(d) == sizeof(int64_t), "");
}
void test_unsigned() {
auto a = 123_u8;
auto b = 123_u16;
auto c = 123_u32;
auto d = 123_u64;
static_assert(sizeof(a) == sizeof(uint8_t) , "");
static_assert(sizeof(b) == sizeof(uint16_t), "");
static_assert(sizeof(c) == sizeof(uint32_t), "");
static_assert(sizeof(d) == sizeof(uint64_t), "");
}
void test_floating() {
auto a = 123_f32;
auto b = 123_f64;
static_assert(sizeof(a) == sizeof(float) , "");
static_assert(sizeof(b) == sizeof(double), "");
}
void test_special() {
auto a = 123_size;
static_assert(sizeof(a) == sizeof(size_t) , "");
}
auto main() -> int {
test_signed();
test_unsigned();
test_floating();
test_special();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment