使用没有std :: literals的后缀

Ank*_*rya 5 c++ language-lawyer c++14

最近,我读了文字就足够像s,h,ms等在C++ 14已投入的命名空间std::literals.因此,如果我要使用它们,那么我应该包含命名空间或用于std::literals::表示这些后缀.然而,当我尝试以下程序(cpp.sh/9ytu)而不使用上述任何一项时,我得到了所需的输出: -

#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
*/
Run Code Online (Sandbox Code Playgroud)

如你所见,我没有包括任何namespacestd::literals::仍然我的程序正确运行.我在Orwell DevC++,C++ Shell,Coliru中尝试了这个,并且到处都有相同的输出.有什么问题?

Col*_*mbo 5

literals并且chrono_literals是内联命名空间 - 在这种特殊情况下请参见[time.syn]:

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

因此,由于using namespace std;找到了所有UDL.

  • @AnkitAcharya所以你可以编写`using namespace std :: literals;`如果你不想输出整个命名空间`std`. (2认同)