Commit 7d48b446b32b762074e0de6d8f22311a88c5c8ef
1 parent
c60b4ea5
Add raw string and user defined literals to c++11 tests
Showing
2 changed files
with
26 additions
and
0 deletions
cSpell.json
libtests/cxx11.cc
| 1 | #include <iostream> | 1 | #include <iostream> |
| 2 | #include <cassert> | 2 | #include <cassert> |
| 3 | #include <cstring> | 3 | #include <cstring> |
| 4 | +#include <cstdlib> | ||
| 4 | #include <functional> | 5 | #include <functional> |
| 5 | #include <type_traits> | 6 | #include <type_traits> |
| 6 | #include <cstdint> | 7 | #include <cstdint> |
| @@ -361,6 +362,29 @@ void do_regex() | @@ -361,6 +362,29 @@ void do_regex() | ||
| 361 | assert((*m3)[2].matched); | 362 | assert((*m3)[2].matched); |
| 362 | } | 363 | } |
| 363 | 364 | ||
| 365 | +static long operator ""_x(char const* v) | ||
| 366 | +{ | ||
| 367 | + return strtol(v, nullptr, 16); | ||
| 368 | +} | ||
| 369 | + | ||
| 370 | +static std::string operator ""_y(char const* v, size_t len) | ||
| 371 | +{ | ||
| 372 | + return "y" + std::string(v, len) + "y"; | ||
| 373 | +} | ||
| 374 | + | ||
| 375 | +void do_user_defined_literals() | ||
| 376 | +{ | ||
| 377 | + assert(10_x == 16); // operator""_x("10") | ||
| 378 | + assert("abc"_y == "yabcy"); // operator""_y("abc", 3) | ||
| 379 | + // Raw literals. Optional matching label before and after () | ||
| 380 | + // allows embedding something that looks like the default end | ||
| 381 | + // delimiter in the string. | ||
| 382 | + assert(R"(abc)"_y == "yabcy"); | ||
| 383 | + | ||
| 384 | + // This construct does not work in MSVC as of version 2019. | ||
| 385 | + // assert(R"x(a)"bc)x"_y == "ya)\"bcy"); | ||
| 386 | +} | ||
| 387 | + | ||
| 364 | int main() | 388 | int main() |
| 365 | { | 389 | { |
| 366 | do_functional(); | 390 | do_functional(); |
| @@ -370,6 +394,7 @@ int main() | @@ -370,6 +394,7 @@ int main() | ||
| 370 | do_default_deleted(); | 394 | do_default_deleted(); |
| 371 | do_smart_pointers(); | 395 | do_smart_pointers(); |
| 372 | do_regex(); | 396 | do_regex(); |
| 397 | + do_user_defined_literals(); | ||
| 373 | std::cout << "assertions passed\n"; | 398 | std::cout << "assertions passed\n"; |
| 374 | return 0; | 399 | return 0; |
| 375 | } | 400 | } |