boost :: shared_ptr和nullptr在默认模板函数参数 [英] boost::shared_ptr and nullptr in default template function argument
本文介绍了boost :: shared_ptr和nullptr在默认模板函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以,这里是我的类及其函数:
So, here is my class and its function:
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;
}
}
}
,它编译没有问题。但是,当我在正常的应用程序中使用它:
That class is in static library, it compliles without problem. However, when I use it in normal application:
cResourceManager<sf::Texture> resourceManager;
resourceManager.add("1.PNG");
我得到错误:
错误: :: shared_ptr'has type'std :: nullptr_t'
I get error: error: default argument for parameter of type ‘boost::shared_ptr’ has type ‘std::nullptr_t’
我不知道这里有什么问题,不能shared_ptr有nullptr值?
我使用g ++ 4.7和-std = c ++ 11
I have no idea what's wrong here, can't shared_ptr have nullptr value? I use g++ 4.7 with -std=c++11
谢谢!
推荐答案
Boost :: shared_ptr将支持std :: nullptr_t自版本1.53起的构造函数。
Boost::shared_ptr will support constructor from std::nullptr_t since version 1.53.
这篇关于boost :: shared_ptr和nullptr在默认模板函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文