R 值插入不适用于 unordered_map

发布于 2024-10-14 14:32:32 字数 1760 浏览 3 评论 0原文

我正在使用存储库中最新的可用 GCC 版本。我决定使用它是因为有一些附加的 C++0x 功能。然而现在我坚持做一些应该有效的事情 - 我想添加新元素以通过 r 值进行映射。简化的代码,演示了问题:

#include <tr1/unordered_map>

class X
{
    public:
        X (void) { /* ... */ };
        X (const X& x) = delete;
        X (X&& x) { /* ... */ };
};

int main (void)
{
    std::tr1::unordered_map<int, X> map;

    // using std::tr1::unordered_map<int, X>::value_type didn't help too
    std::pair<int, X> value (1, X ());

    map.insert (std::move (value));
}

请注意,当用 int 等基本类型替换 X 类时,代码可以编译并正常工作。

在我的生产代码中,对应于 X 的类也没有复制构造函数。

错误消息(像所有与模板相关的错误一样)又长又不可读,我不确定将其放在这里是否是个好主意。如果您想要错误消息,请通知我,以便我更新此问题。消息的最后一部分很有趣:

(...)
/usr/include/c++/trunk/ext/new_allocator.h:106:9: error: use of deleted function ‘constexpr std::pair<_T1, _T2>::pair(const std::pair<_T1, _T2>&) [with _T1 = const int, _T2 = X, std::pair<_T1, _T2> = std::pair<const int, X>]’
In file included from /usr/include/c++/trunk/utility:71:0,
                 from /usr/include/c++/trunk/tr1/unordered_map:34,
                 from kod.cpp:1:
/usr/include/c++/trunk/bits/stl_pair.h:110:17: error: ‘constexpr std::pair<_T1, _T2>::pair(const std::pair<_T1, _T2>&) [with _T1 = const int, _T2 = X, std::pair<_T1, _T2> = std::pair<const int, X>]’ is implicitly deleted because the default definition would be ill-formed:
/usr/include/c++/trunk/bits/stl_pair.h:110:17: error: use of deleted function ‘X::X(const X&)’

此外,这应该有效,因为类似的错误已经修复[ C++0x] 在关联和无序容器中实现 emplace*

也许我做错了什么?在报告之前我想确定这是 GCC 或 libstdc++ 错误。

I'm using the latest available GCC build from repository. I decided to use it because some additional C++0x features. However now I stuck with something what supposes to work - I want to add new element to map via r-value. Simplified code, which demonstrates problem:

#include <tr1/unordered_map>

class X
{
    public:
        X (void) { /* ... */ };
        X (const X& x) = delete;
        X (X&& x) { /* ... */ };
};

int main (void)
{
    std::tr1::unordered_map<int, X> map;

    // using std::tr1::unordered_map<int, X>::value_type didn't help too
    std::pair<int, X> value (1, X ());

    map.insert (std::move (value));
}

Notice, that when replace X class with some primitive type like int code compiles and work just fine.

In my production code class corresponding to X doesn't have copy constructor too.

Error message is (like all template-related errors) long and unreadable and I'm not sure if it's good idea to put it here. Notify me if you want error message, so I'll update this question. Last part of message is interesting:

(...)
/usr/include/c++/trunk/ext/new_allocator.h:106:9: error: use of deleted function ‘constexpr std::pair<_T1, _T2>::pair(const std::pair<_T1, _T2>&) [with _T1 = const int, _T2 = X, std::pair<_T1, _T2> = std::pair<const int, X>]’
In file included from /usr/include/c++/trunk/utility:71:0,
                 from /usr/include/c++/trunk/tr1/unordered_map:34,
                 from kod.cpp:1:
/usr/include/c++/trunk/bits/stl_pair.h:110:17: error: ‘constexpr std::pair<_T1, _T2>::pair(const std::pair<_T1, _T2>&) [with _T1 = const int, _T2 = X, std::pair<_T1, _T2> = std::pair<const int, X>]’ is implicitly deleted because the default definition would be ill-formed:
/usr/include/c++/trunk/bits/stl_pair.h:110:17: error: use of deleted function ‘X::X(const X&)’

Moreover this should work, because similar bug was already fixed [C++0x] Implement emplace* in associative and unordered containers.

Maybe I'm doing something wrong? I want to be sure, that's GCC or libstdc++ bug before reporting it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的往事 2024-10-21 14:32:32

除了使用 tr1 之外,您的代码对我来说看起来是正确的。 tr1 限定的东西不知道右值引用或移动语义。

我获取了您的代码,从标头和命名空间限定符中删除了 tr1,并使用 g++-4.4 和 libc++ (http://libcxx.llvm.org/) 成功编译了您的代码。尝试删除 tr1。

Your code looks correct to me except for your use of tr1. tr1-qualified stuff doesn't know about rvalue reference or move semantics.

I took your code, removed tr1 from the header and namespace qualifiers, and successfully compiled your code using g++-4.4 and libc++ (http://libcxx.llvm.org/). Try removing tr1.

豆芽 2024-10-21 14:32:32

unordered_mapvalue_type 不是 std::pair。它是std::pair。也许如果您使用该类型作为,它会工作得更好。

decltype(map)::value_type value(1, X());
map.insert(std::move(value));

虽然我不明白为什么你的代码不能按原样工作。

The value_type of that unordered_map is not std::pair<int, X>. It is std::pair<const int, X>. Maybe if you use that type for the value it will work better.

decltype(map)::value_type value(1, X());
map.insert(std::move(value));

Although I don't exactly see why your code shouldn't work as is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文