簡體   English   中英

std :: set沒有成員安置

[英]std::set has no member emplace

g ++ 4.7.2是否實現了std::set::emplace ,這是由C ++ 11標准定義並在此處記錄的

我寫了以下小測試用例:

#include <set>
#include <string>

struct Foo
{
    std::string mBar;
    bool operator<(const Foo& rhs) const
    {
        return mBar < rhs.mBar;
    }
    Foo(const std::string bar) : mBar(bar) {};
};

typedef std::set<Foo> Foos;

int main()
{
    Foos foos;
    foos.emplace(std::string("Hello"));
}

在G ++ 4.7.2下,這無法編譯:

[john.dibling@somewhere hacks]$ g++ -o main.o -std=c++0x -c main.cpp
main.cpp: In function ‘int main()’:
main.cpp:19:10: error: ‘Foos’ has no member named ‘emplace’

也無法在IDEOne下編譯,但它在MSVC 2012 Update 1下編譯。

它沒有在gcc 4.7.2中實現。

有一些解釋:

只是澄清一點:這不是疏忽 我們在草案C ++ 0x標准中使用std :: pair時遇到了令人討厭的問題,這實際上使得不可能將emplace_ *成員添加到std :: map,std :: multimap等,而不會破壞現有的用戶代碼。 因此,我們等待,直到整個領域的事情得到澄清。 現在它實際上可以在這些設施上工作。

您的代碼與gcc 4.8.0很好地編譯,請參閱LWS

emplace()關聯容器中加入libstdc++為GCC 4.8.0,GCC下它4.7.2將無法正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM