当前位置: 首页 > 知识库问答 >
问题:

Boost::Container::Map::Emplace()的piecewise_construct在哪里?

董高洁
2023-03-14

使用Boost,我试图将键/值对emplace()转换为Boost::Container::Map。该值需要构造函数的多个参数。根据我所能找到的,我需要使用piecewise_construct并在tuples中传递构造函数参数。这适用于std::map ,但我无法使用Boost::container::map 使其工作。我可以找到的最接近的Boost文档显示Boost::unordered_multimap ,但不是一个普通的映射

#include <map>
#include <boost/tuple/tuple.hpp>
#include <boost/container/map.hpp>
#include <boost/unordered_set.hpp>

class A {
  public:
    /**/    A( int ) { }
    bool    operator<( const A & ) const { return false; }
} ;

class B {
  public:
    /**/    B( int, const char * ) { }
} ;

int
main( int, char ** )
{
    A    a( 100 );
    B    b( 200, "foo" );

    std::map<A,B> mgood;

    mgood.emplace( std::piecewise_construct,
                   std::make_tuple( 100 ),
                   std::make_tuple( 200, "Hello" ) );

#if 1
    boost::container::map<A,B>    mbad;

    mbad.emplace( boost::unordered::piecewise_construct,
                  boost::make_tuple( 300 ),
                  boost::make_tuple( 400, "World" ) );
#endif
}

G++-4.9.2错误消息是无法穿透的(反正对我来说):

make-k tst g++-dboost_log_dyn_link-g-std=c++11-c-o tst.o tst.cc文件包含于/usr/local/include/boost/container/detail/tree.hpp:25:0、来自/usr/local/include/boost/container/map.hpp:30、来自tst.cc:59:/usr/local/include/boost/container/allocator_traits::priv_construct(Boost::move_detail::false_type,cewise_construct_t&,boost::tuples::tuples::tuples::tuple};Allocator=boost::container::new_allocator,void*,(boost::container::tree_type_enum)0u,true>>]–/usr/local/include/boost/container/detail/node_alloc_holder.hpp:167:81:required from–boost::container::container_detail::

null

编译异常退出,代码2于Sat Apr 2 17:11:28

你能给我指出一个有用的方向吗?(我希望不要混合booststd容器;应该可以将emplace放到boost::container::map中,对吗?)

共有2个答案

和光启
2023-03-14

piecewise_construct似乎没有为boost::pair(这是boost::container::map条目的类型)实现。请参阅.../boost/container/detail/pair.hpp:151:

   //piecewise_construct missing
   //template <class U, class V> pair(pair<U, V>&& p);
   //template <class... Args1, class... Args2>
   //   pair(piecewise_construct_t, tuple<Args1...> first_args,
   //        tuple<Args2...> second_args);

我想这方面的实现是很困难的。

云长恨
2023-03-14

COMMIT中添加了对C++03和C++11编译器的piecewise_construct支持:

https://github.com/boostorg/container/commit/79A75F470E75F35F5F2A91E10FCC67D03B0A2160

并将在Boost 1.62中正式发布。下面的代码编译FINE:

#include <boost/tuple/tuple.hpp>
#include <tuple>
#include <boost/container/map.hpp>

class A {
  public:
    /**/    A( int ) { }
    bool    operator<( const A & ) const { return false; }
} ;

class B {
  public:
    /**/    B( int, const char * ) { }
} ;

int main( int, char *[] )
{
    A    a( 100 );
    B    b( 200, "foo" );

    boost::container::map<A,B>    m;

   //1) Both Boost.Tuple and std tuple supported
   //2) std:: or boost::container::piecewise_construct supported
    m.emplace( boost::container::piecewise_construct,
               boost::make_tuple( 300 ),
               boost::make_tuple( 400, "World" ) );

    m.emplace( std::piecewise_construct,
               std::make_tuple( 400 ),
               std::make_tuple( 500, "World2" ) );
}
 类似资料:
  • 我从Boost网站下载Boost 1.54 tar,然后按照此处详细说明进行操作[1]。 特别是,我想在文件夹中安装我的boost库 使用这个: 但在安装后,此文件夹仍为空。为什么? P.S.:最后的升压安装说明: [1] http://www.boost.org/doc/libs/1_54_0/more/getting_started/unix-variants.html

  • 注意:如果重要的话,我不使用IDE,我的大部分编程都使用vim。

  • 本文向大家介绍C ++ STL中的Emplace与Insert,包括了C ++ STL中的Emplace与Insert的使用技巧和注意事项,需要的朋友参考一下 插入操作避免了不必要的对象复制,并且比插入操作更有效地进行了插入。插入操作引用一个对象。 算法 范例程式码 输出结果

  • 根据http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/overview/cpp2011/futures.html,我们可以将boost::asio与一起使用。但是我找不到任何有关使用的信息,它具有更多的功能,例如。我怎么用?

  • 容器内的组件:我们可以在容器内部有多个组件。 语法 (Syntax) 以下是在容器内使用Components的简单语法。 var component1 = Ext.create('Ext.Component', { html:'First Component' }); Ext.create('Ext.container.Container', { renderTo: Ext.getBo

  • Boost 库是一个经过千锤百炼、可移植、提供源代码的 C++ 库,作为标准库的后备,是 C++ 标准化进程的发动机之一。 Boost 库由 C++ 标准委员会库工作组成员发起,在 C++ 社区中影响甚大,其成员已近 2000 人。 Boost 库为我们带来了最新、最酷、最实用的技术,是不折不扣的“准”标准库。