除非定义了B0RKEN,否则以下编译(与命令行中的-DB0RKEN一样):
#include <functional>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
using boost::shared_ptr;
using boost::make_shared;
using my_fn = std::function<void()>;
void foo()
{
my_fn fn = [](){};
#ifdef B0RKEN
shared_ptr<my_fn> k = make_shared<my_fn>(fn);
#else
shared_ptr<int> k = make_shared<int>(0);
#endif
}
Run Code Online (Sandbox Code Playgroud)
似乎提升正在玩一些有趣的游戏,这可能就是为什么这个代码片段有这个问题.我不明白为什么它适用shared_ptr<int>但不是shared_ptr<my_fn>.
我对辩论是否应该使用boost或std共享指针不感兴趣.
我从clang ++中得到以下错误:
foo.cpp:15:24: error: call to 'make_shared' is ambiguous
shared_ptr<my_fn> k = make_shared<my_fn>(fn);
^~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4670:1: note: candidate function [with _Tp =
std::__1::function<void ()>, _Args = <std::__1::function<void ()> &>]
make_shared(_Args&& ...__args)
^
/opt/local/include/boost/smart_ptr/make_shared_object.hpp:246:87: note: candidate function [with T = std::__1::function<void …Run Code Online (Sandbox Code Playgroud)