gpt4 book ai didi

c++ - 'make_shared' 不明确

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:01:32 29 4
gpt4 key购买 nike

除非定义了 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
}

boost似乎在玩一些有趣的游戏,这可能是这段代码出现这个问题的原因。我不明白的是为什么它适用于 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 ()>, Args =
<std::__1::function<void ()> &>]
template< class T, class... Args > typename boost::detail::sp_if_not_array< T >::type make_shared( Args && ... args )
^
1 error generated.

来自 g++:

foo.cpp: In function ‘void foo()’:
foo.cpp:15:45: error: call of overloaded ‘make_shared(my_fn&)’ is ambiguous
shared_ptr<my_fn> k = make_shared<my_fn>(fn);
^
foo.cpp:15:45: note: candidates are:
In file included from PATH_TO_TOOLCHAIN/boost-1.59.0/include/boost/smart_ptr/make_shared.hpp:15:0,
from PATH_TO_TOOLCHAIN/boost-1.59.0/include/boost/make_shared.hpp:15,
from foo.cpp:3:
PATH_TO_TOOLCHAIN/boost-1.59.0/include/boost/smart_ptr/make_shared_object.hpp:246:87: note: typename boost::detail::sp_if_not_array<T>::type boost::make_shared(Args&& ...) [with T = std::function<void()>; Args = {std::function<void()>&}; typename boost::detail::sp_if_not_array<T>::type = boost::shared_ptr<std::function<void()> >]
template< class T, class... Args > typename boost::detail::sp_if_not_array< T >::type make_shared( Args && ... args )
^
In file included from PATH_TO_TOOLCHAIN/gcc-4.9.3/include/c++/4.9.3/memory:82:0,
from PATH_TO_TOOLCHAIN/boost-1.59.0/include/boost/config/no_tr1/memory.hpp:21,
from PATH_TO_TOOLCHAIN/boost-1.59.0/include/boost/smart_ptr/shared_ptr.hpp:23,
from PATH_TO_TOOLCHAIN/boost-1.59.0/include/boost/shared_ptr.hpp:17,
from foo.cpp:2:
PATH_TO_TOOLCHAIN/gcc-4.9.3/include/c++/4.9.3/bits/shared_ptr.h:600:5: note: std::shared_ptr<_Tp1> std::make_shared(_Args&& ...) [with _Tp = std::function<void()>; _Args = {std::function<void()>&}]
make_shared(_Args&&... __args)
^

最佳答案

my_fn 的类型是std::function<void()>; , 驻留在命名空间 std .

当您尝试调用 make_shared<my_fn>(fn); 时它通过 ADL 看到 boost 版本(因为你写了 using boost::make_shared; )和标准版本。

int不属于std make_shared 的命名空间和标准版本不考虑。

尽可能使用限定名称来避免此类问题。

关于c++ - 'make_shared' 不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34532709/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com