2016-01-05 51 views
5

我最近讀到的文字已經放在命名空間std::literals中,像s,hms等等。所以如果我要使用它們,那麼我應該包含名稱空間或使用std::literals::來表示這些後綴。然而,當我嘗試以下程序(cpp.sh/9ytu)不使用任何上述我所需要的輸出: -使用沒有std :: literals的後綴

#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 
*/ 

正如你可以看到我已經不包含在任何namespacestd::literals::還是我的程序正常運行。我試過Orwell DevC++,C++ Shell,Coliru &無處不在。有什麼問題?

+1

你有'使用命名空間標準;'和文字駐留在'標準'裏面的內聯命名空間,不是嗎? –

+1

總是標記「C++」,否則例如語法突出顯示被禁用。 – Columbo

+0

ok @Columbo我會 –

回答

5

literalschrono_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); 
    […] 

因此,由於using namespace std;,所有包括UDL被發現。

+0

那麼wats使用把它們放在其他命名空間而不是'std'本身 –

+2

@AnkitAcharya所以你可以使用namespace std :: literals來編寫'如果你不想傾倒整個命名空間'std'。 – Columbo

+0

@AnkitAcharya:http://stackoverflow.com/q/11016220/179910 –