'x'; // character "some"; // c-style string 7.2f; // float 74u; // unsigned int 74l; // long 0xF8; // hexadecimal number
// OutputType operator "" _suffix(unsigned long long); // OutputType operator "" _suffix(long double);
42_suffix; // OutputType operator "" _suffix(unsigned long long); 42.24_suffix; // OutputType operator "" _suffix(long double);
unsigned long long
argument.long double
argument unsigned long long operator "" _min(unsigned long long minutes) { return minutes * 60; } // ... std::cout << 5_min << std::endl; // 300
OutputType operator "" _suffix(const char* str, size_t size); OutputType operator "" _suffix(const wchar_t* str, size_t size); OutputType operator "" _suffix(const char16_t* str, size_t size); OutputType operator "" _suffix(const char32_t* str, size_t size);
"1234"_suffix; // operator "" _suffix(const char* str, size_t size); u8"1234"_suffix; // operator "" _suffix(const char* str, size_t size); L"1234"_suffix; // operator "" _suffix(const wchar_t* str, size_t size); u"1234"_suffix; // operator "" _suffix(const char16_t* str, size_t size); U"1234"_suffix; // operator "" _suffix(const char32_t* str, size_t size);
std::string
shown below.
std::string operator "" s(const char* str, size_t size) { return std::string(str, size); } // ... std::cout << "some string"s.length() << std::endl;
OutputType operator "" _suffix(const char* literalString);
OutputType operator "" _x(unsigned long long); OutputType operator "" _y(const char*); 1234_x; // call: operator "" _x(1234); 1234_y; // call: operator "" _y("1234");
unsigned long long operator "" _b(const char* str) { unsigned long long result = 0; size_t size = strlen(str); for (size_t i = 0; i < size; ++i) { assert(str[i] == '1' || str[i] == '0'); result |= (str[i] - '0') << (size - i - 1); } return result; } // ... std::cout << 101100_b << std::endl; // 44
template <char...> OutputType operator "" _b();
template <char... bits> struct to_binary; template <char high_bit, char... bits> struct to_binary<high_bit, bits...> { static_assert(high_bit == '0' || high_bit == '1', "Not a binary value!"); static const unsigned long long value = (high_bit - '0') << (sizeof...(bits)) | to_binary<bits...>::value; }; template <char high_bit> struct to_binary<high_bit> { static_assert(high_bit == '0' || high_bit == '1', "Not a binary value!"); static const unsigned long long value = (high_bit - '0'); }; template <char... bits> constexpr unsigned long long operator "" _b() { return to_binary<bits...>::value; } // ... int arr[1010_b]; // std::cout << 101100_b << std::endl; // 44
operator "" _x (unsigned long long)
or operator "" _x (long double)`
operator "" _x (const char* raw)
operator "" _x <'c1', 'c2', ... 'cn'>
long operator "" f(long double value) { return long(value); } // ... std::cout << 42.7f << std::endl; // 42.7
Speed sp1 = 100m / 9.8s; // very fast for a human Speed sp2 = 100m / 9.8s2; // error (m/s2 is acceleration) Speed sp3 = 100 / 9.8s; // error (speed is m/s and 100 has no unit)
Source: https://habr.com/ru/post/140357/