本文介绍了升压unordered_map ::不可复制不能返回从运营商文献[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要证明我的问题,考虑这个简单的程序,不能编译:

To demonstrate my problem, consider this simple program that does not compile:

#include <boost/noncopyable.hpp>
#include <unordered_map>

class foo : boost::noncopyable { };

int main()
{
    std::unordered_map<int, foo> m;
    auto & element = m[0];

    return 0;
}

使用boost的当前版本(1.52)时,Visual Studio 2012将返回错误:
不能访问类的声明提振私有成员::不可复制_ ::不可复制

运算符[]为的std :: unordered_map在返回所提供的密钥,乍一看好像它应该工作的一个参考因素 - 我要求的元素,而不是它的一个副本的引用

The operator [] for std::unordered_map returns a reference to the element at the provided key, which at first glance seems like it should work -- I've asked for a reference to the element, not a copy of it.

我对这个问题的理解是这样的(这可能是错误的,因为我没有在使用了一段时间C ++)。如果没有找到关键,unordered_map创建一个新的元件,并返回到新的元素的引用。提高:: noncopyable进行定义(私有)拷贝构造函数而不是移动构造函数,所以不能由编译器生成一个移动构造函数。在它的操作符[]的std :: unordered_map使得使用的std ::举动,但由于升压:: noncopyable进行没有定义移动构造函数,它退到拷贝构造函数。由于拷贝构造函数是私有的,编译失败。

My understanding of the problem is this (which might be wrong, as I haven't used C++ in a while). If the key is not found, unordered_map creates a new element and returns a reference to the new element. boost::noncopyable defines a (private) copy constructor but not a move constructor, and so a move constructor is not generated by the compiler. In its operator[], std::unordered_map makes use of std::move, but since boost::noncopyable doesn't define a move constructor, it falls back to the copy constructor. Since the copy constructor is private, the compilation fails.

是什么促使这个帖子,我试图创建的boost ::信号2 ::信号,它从boost :: noncopyable进行继承的unordered_map。短黑客Boost库中,有一个简单的解决办法,我可以做什么?在包装的unique_ptr信号是一种选择,但在我看来,我可能是错在这里做一些事情。

What prompted this post is that I'm trying to create an unordered_map of boost::signal2::signal, which inherits from boost::noncopyable. Short of hacking the boost library, is there a simple workaround I can do? Wrapping the signal in a unique_ptr is an option, but it seems to me I might be doing something wrong here.

也许是我太即将发布!这似乎不可能增加的boost ::不可复制到unordered_map的子类。插入,操作符[]和布设都使用一个拷贝构造函数(即私有),或移动操作(不升压:: noncopyable进行存在)。对我来说,这似乎是一个主要的限制。它甚至有可能创建一个包含的boost ::不可复制对象的unordered_map?我很明确的的尝试复制它们 - 我希望他们花自己的整个生命周期的unordered_map内

I may have posted too soon! It appears impossible to add a subclass of boost::noncopyable to unordered_map. Insert, operator[], and emplace all use either a copy constructor (which is private), or a move operation (which doesn't exist for boost::noncopyable). To me this seems a major limitation. Is it even possible to create an unordered_map that contains boost::noncopyable objects? I'm explicitly not trying to copy them -- I want them to spend their entire lifespan inside the unordered_map.

推荐答案

这不是不可能使用的子类的boost ::不可复制 unordered_map ,您只需为你键入定义移动构造函数。如果你做了自己的副本构造(这是的boost ::不可复制一样)C ++不创建一个默认的构造函数的举动。另外,如果它没有定义默认构造函数的举动,它会尝试调用父类的拷贝构造函数是私有的。所以,你必须定义一个移动构造函数不尝试调用的boost ::不可复制的拷贝构造函数。例如,这工作得很好:

It's not impossible to use a subclass of boost::noncopyable in an unordered_map, you simply have to define a move constructor for you type. C++ does not create a default move constructor if you've made your own copy construct (which is what boost::noncopyable does). Also, if it did define default move constructor, it would try to call the parent's copy constructor which is private. So you must define a move constructor that doesn't try to call boost::noncopyable's copy constructor. For example this works fine:

#include <boost/noncopyable.hpp>
#include <unordered_map>

struct foo : public boost::noncopyable
{
    foo() = default;
    foo(foo&&) {}
};

int main()
{
    std::unordered_map<int, foo> m;
    auto & element = m[0];

    return 0;
}

这篇关于升压unordered_map ::不可复制不能返回从运营商文献[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 08:38