Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using suffixes without std::literals

I recently read that the literal suffices like s, h, ms etc in C++14 have been put in the namespace std::literals. So if I'm to use them then I should either include the namespace or use std::literals:: to denote those 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 haven't included any namespace or std::literals:: still my program runs correctly. I tried this in Orwell DevC++, C++ Shell, Coliru & got the same output everywhere. What's the issue ?

like image 433
Ankit Acharya Avatar asked Jan 05 '16 18:01

Ankit Acharya


1 Answers

literals and chrono_literals are inline 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 are found.

like image 183
Columbo Avatar answered Sep 26 '22 17:09

Columbo