Emplace pointer to multimap shared_ptr not working

Vector works correctly

Header
std::vector<std::shared_ptr<SceneNode>> subnodes_m;

Definition
void CompositeSceneNode::AddChild(SceneNode* subnode_p)
{
    subnodes_m.emplace_back(subnode_p);
}

Multimap not

Header
std::multimap<unsigned int, std::shared_ptr<SceneNode>> subnodes_m;

Definition
void CompositeSceneNode::AddChild(SceneNode* subnode_p, unsigned int layerIndex)
{
    subnodes_m.emplace(layerIndex, subnode_p);
}

I get the following error:

error C2664: 'std::pair<_Ty1,_Ty2>::pair(const unsigned int &,const _Ty2 &)' :
cannot convert parameter 2 from 'RendererD3DWrapper::SceneNode *'
to 'const std::shared_ptr<_Ty> &'   

Does anyone have a key?

+4
source share
1 answer

You cannot build std::pair<T1,T2>with type arguments Uand Vunless there is an implicit conversion Uto T1and Vfrom T2. In your case there is no implicit conversion SceneNode*to std::shared_ptr<SceneNode>.

From the C ++ standard:

ยง 20.3.2 Class template pair [pairs.pair]

template<class U, class V> constexpr pair(U&& x, V&& y);
  1. Requires: is_constructible<first_type, U&&>::valueis trueand is_constructible<second_type, V&&>::valueis true.

  2. : first std::forward<U>(x) second std::forward<V>(y).

  3. : U first_type V, second_type, .

, std::pair<T1,T2>, ( emplace in-place a std::pair<key_type, mapped_type>, value_type std::multimap):

std::pair<unsigned int, std::shared_ptr<SceneNode>> p( 1, new SceneNode );

std::shared_ptr<T>, ( ), explicit, , , :

ยง 20.9.2.2 shared_ptr [util.smartptr.shared]

[...]

template<class Y> explicit shared_ptr(Y* p);

++ 11 std::shared_ptr<T> emplace:

subnodes_m.emplace(layerIndex, std::shared_ptr<SceneNode>(subnode_p));

( std::pair<T1,T2>), :

subnodes_m.emplace(std::piecewise_construct
                 , std::forward_as_tuple(layerIndex)
                 , std::forward_as_tuple(subnode_p));

DEMO

std::vector std::shared_ptr, ?

std::vector<std::shared_ptr<T>>::emplace_back emplace_back std::shared_ptr<T>, . map a multimap, emplaced - pair, , , ( ).

+10

All Articles