jeudi 31 mars 2016

Argument forwarding for emplace() makes constructor arguments const

I'm trying to use emplace() to construct in-place a map<K,V> entry (using Boost). The key object constructor arg gets forwarded through the template magic correctly, but the V object constructor arg becomes const, so it doesn't work.

#include <boost/container/map.hpp>

class A {
  public:
    /**/     A( int n ) { }
    friend bool operator<( const A &a1, const A &a2 ) { return false; }
} ;

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

class C {
  public:
    /**/     C( B &b ) { }
} ;

int
main( int, char ** )
{
    boost::container::map<A,B>   m1;
    boost::container::map<A,C>   m2;
    B                            b( "Foo" );
    C                            c( b ); // <--- this works OK.

    m1.emplace( 1, "Hello" );
    m2.emplace( 2, b ); // <----- this fails!
}

The Error is:

Error: /usr/local/include/boost/container/detail/pair.hpp:128:38: error: no matching function for call to C::C(**const** B&), second(::boost::forward<V>(v))

Something about the emplace argument-forwarding turns *b* into *const b* in the last line. I know there must be a boost::bla_bla_bla that I can apply to make it work, but I haven't been able to find it.

Can anybody help?

Aucun commentaire:

Enregistrer un commentaire