默认模板函数参数中的 boost::shared_ptr 和 nullptr

boost::shared_ptr and nullptr in default template function argument

本文关键字:shared ptr nullptr boost 函数 参数 默认      更新时间:2023-10-16

所以,这是我的类及其函数:

template <class T>
class cResourceManager
{
public:
    bool add(const std::string & key, boost::shared_ptr<T> ptr = nullptr); 
private:
    std::map <const std::string, boost::shared_ptr<T> > resources;
};
template <class T>
bool cResourceManager<T>::add(const std::string & key, boost::shared_ptr<T> ptr)
{
    if (resources.find(key) == resources.end())
    {
        if(ptr != nullptr) //we have the object
        {
            resources.insert(std::pair<const std::string, boost::shared_ptr<T>>(key, ptr));
        return true;
    }
    else //we need to load object using sfml loadFromFile
    {
        T tempResource;
        tempResource.loadFromFile(key);
        resources.insert(std::pair<const std::string, boost::shared_ptr<T>>(key, new T(tempResource)));
        if(resources[key] == nullptr) return false;
        else return true;
    }
}
}     

该类在静态库中,它可以毫无问题地遵守。但是,当我在正常应用程序中使用它时:

cResourceManager<sf::Texture> resourceManager;
resourceManager.add("1.PNG");

我收到错误:错误:类型为"boost::shared_ptr"的参数的默认参数具有"std::nullptr_t"类型。

我不知道这里出了什么问题,shared_ptr不能有 nullptr 值吗?我将 g++ 4.7 与 -std=c++11 一起使用

谢谢!

Boost:

:shared_ptr 将从 1.53 版本开始支持 std::nullptr_t 的构造函数。

我认为

这将适用于 std::shared_ptr 而不是 boost::shared_ptr

根据 http://www.boost.org/doc/libs/1_52_0/libs/smart_ptr/shared_ptr.htm#Members,boost::shared_ptr不能由空指针常量初始化,因为对于接受指针的构造函数,它由Y*模板化,其中Y是模板参数。在推导Y*时不考虑将空指针常量转换为Y*,因此构造函数将出现推导失败,并且在传递nullptr时被忽略。

但是std::shared_ptr可以接受它,因为它具有std::nullptr_t过载,所以如果你愿意,你可以切换。

请注意,传递nullptr不同于传递空指针(如 (T*)nullptr )。后者不会使用 constexpr 构造函数,但前者会(以及其他差异)。因此,在前一种情况下,如果您的指针是命名空间范围变量,则它具有常量初始化,并且不会与其他翻译单元中的命名空间范围对象引起初始化竞争。