相关疑难解决方法(0)

为什么unique_ptr在shared_ptr只占用一个时会占用两个模板参数?

双方unique_ptrshared_ptr接受定制的析构函数他们所拥有的对象上调用.但在的情况下unique_ptr,析构函数作为一个模板参数传递,而类型shared_ptr的自定义析构函数将被指定为一个模板参数的构造函数.

template <class T, class D = default_delete<T>> 
class unique_ptr
{
    unique_ptr(T*, D&); //simplified
    ...
};
Run Code Online (Sandbox Code Playgroud)

template<class T>
class shared_ptr
{
    template<typename D>
    shared_ptr(T*, D); //simplified
    ...
};
Run Code Online (Sandbox Code Playgroud)

我不明白为什么会有这样的差异.需要什么?

c++ std shared-ptr unique-ptr c++11

63
推荐指数
1
解决办法
3714
查看次数

为什么C++ 11不能将不可复制的仿函数移动到std :: function?

//------------------------------------------------------------------------------
struct A
{
    A(){}
    A(A&&){}
    A& operator=(A&&){return *this;}
    void operator()(){}

private:
    A(const A&);
    A& operator=(const A&);

    int x;
};

//------------------------------------------------------------------------------
int main()
{
    A a;
    std::function<void()> func(std::move(a));
}
Run Code Online (Sandbox Code Playgroud)

'A :: A':无法访问类'A'中声明的私有成员

似乎当我通过引用捕获某些东西时,或者const我可以制作一个不可复制的lambda.但是,当我这样做时,它实际上是为了给它一个std::function.

c++ lambda function c++11

18
推荐指数
1
解决办法
2223
查看次数

从字符串/ boost :: any map构建boost :: options

我有一张代表配置的地图.这是地图std::stringboost::any.

此映射在开始时初始化,我希望用户能够在命令行上覆盖这些选项.

我喜欢做的是使用该options_description::add_option()方法从此地图构建程序选项.但是,它需要一个模板参数,po::value<>而我只有boost::any.

到目前为止,我只是拥有代码的shell. m_Config代表我的配置类,并getTuples()返回一个std::map<std::string, Tuple>. TuplePair是一个typedef std::pair<std::string, Tuple>和Tuple包含boost::any我感兴趣的.

    po::options_description desc;
    std::for_each(m_Config.getTuples().begin(),
                  m_Config.getTuples().end(),
                  [&desc](const TuplePair& _pair)
    {
            // what goes here? :)
            // desc.add_options() ( _pair.first, po::value<???>, "");
    });
Run Code Online (Sandbox Code Playgroud)

有没有办法以这种方式构建它,还是我需要自己去做?

提前致谢!

c++ boost-program-options boost-any

9
推荐指数
1
解决办法
2112
查看次数

shared_ptr存储删除器如何?

我无法理解如何shared_ptr存储我给它的删除器.

最初,使用a shared_ptr<int>,我认为它可能会使用a std::function<void(int*)>,但我可以作为删除器给出任何类型的函数(或可调用对象),只要第一个参数是a int*.

怎么shared_ptr办呢?

我很抱歉,如果这是一个愚蠢的问题,我是C++的新手,请原谅我!

编辑:问题是:我怎么能做那样的事情?我该怎么用?任何例子?或者这是一个非常高级的话题?

c++ generics function shared-ptr

8
推荐指数
2
解决办法
498
查看次数