Using suffixes without std :: litals

I recently read that a literal such as s , h , ms , etc. was placed in the std::literals namespace in C ++ 14. Therefore, if I use them, I must either include the namespace or use std::literals:: to denote these suffixes. However, when I tried the following program ( cpp.sh/9ytu ) without using any of the above, I got the required output: -

 #include <iostream> #include <thread> using namespace std; int main() { auto str = "He is there"s; auto timegap = 1s; cout << "The string is :-" << endl; this_thread::sleep_for(timegap); cout << str; return 0; } /*Output:- The string is :- He is there */ 

As you can see, I did not include any namespace or std::literals:: while my program was working correctly. I tried this in Orwell DevC ++, C ++ Shell, Coliru and got the same result all over the world. What is the problem?

+5
source share
1 answer

literals and chrono_literals are internal namespaces - see [time.syn] in this particular case:

 inline namespace literals { inline namespace chrono_literals { // 20.12.5.8, suffixes for duration literals constexpr chrono::hours h(unsigned long long); […] 

Therefore, due to using namespace std; all UDLs found.

+5
source

Source: https://habr.com/ru/post/1239922/


All Articles