草庐IT

make_shared

全部标签

C++ 依赖注入(inject)——通过引用还是通过 boost::shared_ptr?

在需要构造函数依赖注入(inject)的情况下,使用引用注入(inject)与使用boost::shared_ptr注入(inject)有哪些注意事项?还有其他常见的方法吗?它与上述两种方法相比如何? 最佳答案 您可以选择如何管理要注入(inject)的对象的生命周期。整体架构可能会决定哪种选择最有意义。有了引用,更高层次的东西必须管理对象的生命周期;使用shared_ptr将自动管理生命周期。 关于C++依赖注入(inject)——通过引用还是通过boost::shared_ptr?,

c++ - 为什么 make_unique 有一个可以将 std::bind 作为参数的构造函数的额外移动?

我有一个简单的类,它的构造函数如下所示:Event(std::function&&f):m_f(std::move(f)){}构造函数可以与std::bind一起使用:Thingthing;std::unique_ptrev(newEvent(std::bind(some_func,thing)));以上述方式使用它会导致“事物”的一个拷贝构造,然后在该拷贝上进行移动构造。但是,执行以下操作:std::unique_ptrev=make_unique(std::bind(some_func,thing));导致两个移动结构。我的问题是:什么时候调用“thing”的移动构造函数为什么用m

c++ - 通过继承扩展 shared_ptr

做这样的事情有什么不好?classmyclass:publicstd::shared_ptr{//somecode,anallocationisneverdonestd::stringget_info(){if(*this!=nullptr)return""+(this->info*3)+"";elsereturn"";}};当类中没有分配时---只是像上面那样提供一些装饰? 最佳答案 原则上允许从STL类派生,参见here和here.但是,您必须知道您不应该使用指向基类的指针——即std::shared_ptr*。在这种情况下。所

C++11 闭包 shared_ptr

创建由shared_ptr管理的堆分配闭包的语法是什么。我想将闭包传递给函数并能够传递nullptr。似乎使用了shared_ptr但我无法理解从lambda表达式初始化它的语法 最佳答案 应该是这样的autolambda=[](){/*dosomethingusefull*/};autop=std::make_shared>(lambda);但实际上你可能不需要shared_ptr,因为function可以从nullptr构造。std::functionfnc(nullptr); 关于

c++ - 为什么 std::make_move_iterator 适用于 vector<string> 但不适用于 vector<int>

我期待std::make_move_iterator总是会move内容,但似乎不会。看起来是在vector中move元素但不在vector.请看下面的代码片段:#include#include#include#includevoidmoveIntVector(){std::coutv1;for(unsignedi=0;iv2(std::make_move_iterator(v1.begin()+5),std::make_move_iterator(v1.end()));std::coutv1;for(unsignedi=0;iv2(std::make_move_iterator(v1.

报错:git clone 时候出现Please make sure you have the correct access rights and the repository exists.问题解决

输入gitclone命令时出现Pleasemakesureyouhavethecorrectaccessrightsandtherepositoryexists.错误,出现改问题的原因是git服务器没有存储本地ssh密钥。解决步骤:删除.ssh文件夹【C:\Users(本地用户名).ssh】中的known_hosts(直接删除即可)在下载好的Git中的bin目录下(一般是C:\ProgramFiles\Git\bin)打开bash.exe输入命令ssh-keygen-trsa-C“username”(注:username为你git上的用户名),如果执行成功。返回:Generatingpubli

c++ - make_pair 命名空间污染

在我最近编写的代码中,我注意到一个奇怪的行为。当我使用第一个参数为std::pair的make_pair时,make_pair变得“神奇地”在命名空间中可用(我不必使用std::限定符)#includeintmain(){inti1=2;inti2=10;inti3=0;//constructingapairusingstd::make_pair,everything'sokaystd::pairkey=std::make_pair(i1,i2);//here,whyismake_pairsuddenlymagicallyavailablewithoutthe//std::namesp

c++ - std::make_shared 与 throw dtor 和 libc++ 不编译

这是非常基本的代码:#includeclassfoo{public:~foo()noexcept(false){}};intmain(){autox=std::make_shared();return0;}编译如下:g++-std=c++11test.cpp当使用libc++编译时,它会失败:/usr/bin/../include/c++/v1/memory:3793:7:error:exceptionspecificationofoverridingfunctionismorelaxthanbaseversionclass__shared_ptr_emplace^/usr/bin/.

CMake Error at /usr/local/share/cmake-3.24/Modules/FindCUDA.cmake:859 (message): Specify CUDA_TOOL

问题从错误日志中可以看到,问题出在CMake无法找到CUDA工具包的根目录。错误消息是:CMakeErrorat/usr/local/share/cmake-3.24/Modules/FindCUDA.cmake:859(message):SpecifyCUDA_TOOLKIT_ROOT_DIR这意味着CMake需要知道CUDA工具包的安装位置,以便正确配置和构建denseflow。解决方式1为了解决这个问题,你需要设置CUDA_TOOLKIT_ROOT_DIR环境变量,指向CUDA的安装目录。通常,CUDA安装在/usr/local/cuda目录,但这可能因系统而异。你可以通过以下命令设置C

c++ - 从 unique_ptr<T[]> 初始化 shared_ptr<T>

[跟进this问题]最近我一直在处理指向C风格数组的智能指针。我最终完成了推荐的事情并改为使用指向vector的智能指针,但在那段时间里,我得到了一些建议:不要使用shared_ptr对象来管理最初使用make_unique创建的数组因为它不会调用delete[]而是delete.这对我来说似乎不合逻辑,我检查了两个Coliru和标准:这段代码:#include#includeintmain(){std::coutmyUnique(customArrayAllocator(4),customArrayDeleter);std::coutmyShared=std::move(myUniq