Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ std::shared_ptr与自定义删除程序的Typedef别名_C++_Templates_C++14 - Fatal编程技术网

C++ std::shared_ptr与自定义删除程序的Typedef别名

C++ std::shared_ptr与自定义删除程序的Typedef别名,c++,templates,c++14,C++,Templates,C++14,我想使用自定义删除器为std::shared_ptr创建一个别名 此代码可以工作,但仅适用于唯一指针。对于标记为[1]的行,我得到了关于模板参数数量无效的错误 我注意到,std::unique_ptr和std::shared_ptr的模板参数和ctor参数不同,如下所示: 我注意到这个问题可能是重复的,但我不知道如何解决我的问题 #include <memory> #include <iostream> template<class T> struct De

我想使用自定义删除器为
std::shared_ptr
创建一个别名

此代码可以工作,但仅适用于唯一指针。对于标记为[1]的行,我得到了关于模板参数数量无效的错误

我注意到,
std::unique_ptr
std::shared_ptr
的模板参数和ctor参数不同,如下所示:

我注意到这个问题可能是重复的,但我不知道如何解决我的问题

#include <memory>
#include <iostream>

template<class T>
struct Deleter {
    void operator()(T* p) const noexcept {
       p->Drop(); // SFINAE
    };
};

template <class T>
using my_unique_ptr = std::unique_ptr<T, Deleter<T>>;

//template <class T>
//using my_shared_ptr = std::shared_ptr<T, Deleter<T>>; // [1] does not work
//using my_shared_ptr = std::shared_ptr<my_unique_ptr<T>>; // this is pointless

template <class T>
using my_shared_ptr = std::shared_ptr<T>;


template <class T, class... Args>
my_unique_ptr<T> my_make_unique(Args&&... args)
{
    return my_unique_ptr<T>(new T(std::forward<Args>(args)...));
}

template <class T, class... Args>
std::shared_ptr<T> my_make_shared(Args&&... args)
{
    return {new T(std::forward<Args>(args)...), Deleter<T>{}};
//  return {new T(std::forward<Args>(args)...), Deleter<T>()}; this also works
}

class MyClass{
    public:
      MyClass() 
      {
          std::cout << "Default ctor\n";
      }
      ~MyClass()
      {
          std::cout << "Default dtor\n";
      }
      void Drop()
      {
          std::cout << "Custom deleter\n";
          delete this;
      }
};

int main()
{
    {
        my_unique_ptr<MyClass> p1(new MyClass);
        my_unique_ptr<MyClass> p2 = my_make_unique<MyClass>();
    }

    {
//      my_shared_ptr<MyClass> p(new MyClass) // [2] does not work
//      my_shared_ptr<MyClass> p(my_make_unique<MyClass>()); // [3] does not work
        std::shared_ptr<MyClass> p1 = my_make_shared<MyClass>(); // [4] works
        my_shared_ptr<MyClass> p2 = my_make_shared<MyClass>();
    }
}
理解
std::unique\u ptr
的ctor是显式的,但
std::shared\u ptr
的ctor不是


非常感谢您的帮助您可以执行
my\u make\u shared
,例如:

template <class T, class... Args>
std::shared_ptr<T> my_make_shared(Args&&... args)
{
    return {new T(std::forward<Args>(args)...), Deleter<T>{}};
}
模板
std::shared_ptr my_make_shared(Args&&…Args)
{
返回{newt(std::forward(args)…),Deleter{};
}
用法:

 std::shared_ptr<MyClass> p(my_make_shared<MyClass>());
std::shared_ptr p(my_make_shared());
对于[3],它应该是:

 std::shared_ptr<MyClass> p(my_make_unique<MyClass>());
std::shared_ptr p(my_make_unique());

请注意,
std::shared_ptr
几乎毫无意义。

(1)请发布错误消息,(2)看起来您在[1]中忘记了一个右括号,
,(3)我在任何地方都看不到
typedef
shared_ptr
实际上是删除了它的类型。它不是一个模板参数。请看复制的接口,也许。@Rakete1111已修复,尽管它仍然没有编译。我为您上次的编辑粘贴了错误更正:为两个智能指针使用单个指针的构造函数是显式的
{}
避免最麻烦的解析,禁止缩小参数范围<代码>()
在C++11之前工作,看起来像一个函数调用(因此在某些情况下可以被函数或宏替代)。请添加示例代码如何使用它创建对象,我无法让它同时使用[2]和[3]@Xeverus:Ex添加。您能解释我最后一个问题中的问题吗?@Xeverus:std::unique的构造函数\u ptr获取指针是显式的,所以不允许使用这种语法。这是为了避免隐式转换,这将是非常危险的。在您的情况下,您可以使用
return{newt(std::forward(args)…,{}}
(它构建了一个默认的
Deleter
)。只取指针的也是
显式的
,取
std::unique\u ptr&
的是不显式的并且是安全的(所有权转移)
template <class T, class... Args>
std::shared_ptr<T> my_make_shared(Args&&... args)
{
    return {new T(std::forward<Args>(args)...), Deleter<T>{}};
}
 std::shared_ptr<MyClass> p(my_make_shared<MyClass>());
 std::shared_ptr<MyClass> p(my_make_unique<MyClass>());