默认模板函数参数中的 boost::shared_ptr 和 nullptr
boost::shared_ptr and nullptr in default template function argument
所以,这是我的类及其函数:
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
构造函数,但前者会(以及其他差异)。因此,在前一种情况下,如果您的指针是命名空间范围变量,则它具有常量初始化,并且不会与其他翻译单元中的命名空间范围对象引起初始化竞争。
相关文章:
- CLANG 编译器 说:变量"PTR"可能未初始化
- 在以唯一ptr为值的C++映射中,动态内存何时会被销毁
- 将 ptr 传递给 ptr 到 A 作为参数传递给 A 的函数是不好的做法吗?
- 为共享 ptr 向量实现复制 c'tor?
- 字符和整数中 **(ptr+1) 的值差异
- C++:在不中断共享的情况下通过引用传递共享 PTR?
- 如何将派生类从基 ptr 分配给 nlohmann::json
- 引用 std::shared:ptr 以避免引用计数
- 为什么我不能在不进行任何转换的情况下将浮点数放入任何类型的 ptr 中?
- 在调用函数时,ptr** 和 ptr*& 之间是否有区别,或者首选C++?
- 另一种类型的智能ptr,比如具有弱refs的unique_ptr
- 尝试打印出 *ptr++ 的值,以了解它是如何工作的
- 如何控制共享 ptr 引用计数?
- dopen():不以 root 身份运行时"failed to map segment from shared object"
- C++中的指针否定 (!ptr == NULL)
- 从const ptr*转换为ptr*时出现问题
- 无法使用 libtool 将 -shared 参数传递给 g++
- boost::shared_ptr和std::shared-ptr的同居
- 我可以用std::shared_ptr而不是boost::shared-ptr构建boost库吗
- shared-ptr-C++shared_ptr与unique_ptr用于资源管理