草庐IT

Make_shared

全部标签

c++ - 为什么 std::make_tuple(7 + N...) 在 C++11 中是合法的?

以下代码在C++11中是合法的。templatestd::tuplef(){returnstd::make_tuple(7+N...);}什么意思? 最佳答案 首先看模板参数:template.即使可以将可变数量的模板参数提供给f,它们都必须是int类型.现在当您使用f,parameterunpacking(7+N...)将遵循模式7+N并展开为7+t1,7+t2,7+t3,...,7+tn因此,您最终会得到一个元组,其中包含的每个模板参数都增加了7。详细信息可以在第14.5.3节可变参数模板[temp.variadic]中找到。3

c++ - 为什么不使用 `make_x()` 函数尽可能省略 move 构造函数?

我不知道为什么在最后一种情况下是在启用复制省略时调用move构造函数(甚至是强制性的,例如在C++17中):classX{public:X(inti){std::clogXmake_X(T&&arg){returnX(std::forward(arg));}intmain(){autox1=make_X(1);//1xconvertingctorinvokedautox2=X(X(1));//1xconvertingctorinvokedautox3=make_X(X(1));//1xconvertingand1xmovectorinvoked}在这种情况下,哪些规则会阻碍move构造

c++ - 为什么不使用 `make_x()` 函数尽可能省略 move 构造函数?

我不知道为什么在最后一种情况下是在启用复制省略时调用move构造函数(甚至是强制性的,例如在C++17中):classX{public:X(inti){std::clogXmake_X(T&&arg){returnX(std::forward(arg));}intmain(){autox1=make_X(1);//1xconvertingctorinvokedautox2=X(X(1));//1xconvertingctorinvokedautox3=make_X(X(1));//1xconvertingand1xmovectorinvoked}在这种情况下,哪些规则会阻碍move构造

c++ - 在 dll 接口(interface)中使用 shared_ptr

我的dll中有一个抽象类。classIBase{protected:virtual~IBase()=0;public:virtualvoidf()=0;};我想在加载dll的exe文件中获取IBase。第一种方法是创建以下函数IBase*CreateInterface();并在IBase中添加虚函数Release()。第二种方法是创建另一个函数boost::shared_ptrCreateInterface();并且不需要Release()函数。问题。1)在第二种情况中,在dll(不是在exe文件中)调用析构函数和内存释放是真的吗?2)如果exe文件和dll使用不同的编译器(或不同的设

c++ - 在 dll 接口(interface)中使用 shared_ptr

我的dll中有一个抽象类。classIBase{protected:virtual~IBase()=0;public:virtualvoidf()=0;};我想在加载dll的exe文件中获取IBase。第一种方法是创建以下函数IBase*CreateInterface();并在IBase中添加虚函数Release()。第二种方法是创建另一个函数boost::shared_ptrCreateInterface();并且不需要Release()函数。问题。1)在第二种情况中,在dll(不是在exe文件中)调用析构函数和内存释放是真的吗?2)如果exe文件和dll使用不同的编译器(或不同的设

c++ - unique_ptr 与 shared_ptr 中的删除器类型

这个问题在这里已经有了答案:Whydoesunique_ptrtaketwotemplateparameterswhenshared_ptronlytakesone?(2个回答)关闭7年前。当我发现标准以两种完全不同的方式定义了std::unique_ptr和std::shared_ptr时,我觉得这非常奇怪可能拥有。这是来自cppreference::unique_ptr的声明和cppreference::shared_ptr:template>classunique_ptr;templateclassshared_ptr;如您所见,unique_ptr将Deleter对象的类型“保

c++ - unique_ptr 与 shared_ptr 中的删除器类型

这个问题在这里已经有了答案:Whydoesunique_ptrtaketwotemplateparameterswhenshared_ptronlytakesone?(2个回答)关闭7年前。当我发现标准以两种完全不同的方式定义了std::unique_ptr和std::shared_ptr时,我觉得这非常奇怪可能拥有。这是来自cppreference::unique_ptr的声明和cppreference::shared_ptr:template>classunique_ptr;templateclassshared_ptr;如您所见,unique_ptr将Deleter对象的类型“保

c++ - 你能分配一个与 make_shared 等效的数组吗?

buffer=newchar[64];buffer=std::make_shared(char[64]);???你能用make_shared()为数组分配内存吗??我可以:buffer=std::make_shared(newchar[64]);但这仍然涉及调用new,据我了解make_shared更安全、更高效。 最佳答案 您需要共享分配的内存吗?您可以改用std::unique_ptr和std::make_unique在C++14上可用:autobuffer=std::make_unique(64);会有一个std::make_

c++ - 你能分配一个与 make_shared 等效的数组吗?

buffer=newchar[64];buffer=std::make_shared(char[64]);???你能用make_shared()为数组分配内存吗??我可以:buffer=std::make_shared(newchar[64]);但这仍然涉及调用new,据我了解make_shared更安全、更高效。 最佳答案 您需要共享分配的内存吗?您可以改用std::unique_ptr和std::make_unique在C++14上可用:autobuffer=std::make_unique(64);会有一个std::make_

c++ - 将带有自定义删除器的 unique_ptr 移动到 shared_ptr

我有一个函数可以创建一个带有自定义删除器的unique_ptr并返回它:autogive_unique_ptr(){autodeleter=[](int*pi){deletepi;};int*i=newint{1234};returnstd::unique_ptr(i,deleter);}在该函数的客户端代码中,我想将unique_ptr移动到shared_ptr中,但鉴于我不知道该怎么做在函数之外不知道我的自定义删除器的decltype。我猜它应该是这样的:autouniquePtr=give_unique_ptr();autosharedPtr=std::shared_ptr(st