std::shared_ptr and initializer lists

前端 未结 3 725
广开言路
广开言路 2020-12-09 16:37

The std::shared_ptr constructor isn\'t behaving as I expected:

#include 
#include 

void func(std::vector st         


        
3条回答
  •  孤城傲影
    2020-12-09 17:12

    You need to use make_shared if you want to create a new object, constructed from those arguments, pointed to by a shared_ptr. shared_ptr is like a pointer to T- it needs to be constructed with a pointer to T, not a T.

    Edit: Perfect forwarding is in fact not at all perfect when initializer lists are involved (which is the suck). It's not a bug in the compiler. You will have to create an rvalue of type Func manually.

提交回复
热议问题