I am developing a Factory that creates different types of Foo, and I'm trying to use smart pointers.
Most of them seem to work well, but I miss some important functions (namely nullptr) due to compiler limitations.
I have this method:
std::unique_ptr<Foo> createFoo(const std::string &fooType) {
auto it = _registeredFoo.find(fooType);
if(it != _registeredFoo.end())
return std::unique_ptr<Foo>(it->second());
return std::unique_ptr<Foo>(NULL);
}
When I test this method, it never returns a pointer NULL.
This is how I test my method.
std::unique_ptr<Foo> _foo = FooFactory::getInstance()->createFoo("FOO");
if(_foo) {
std::cout << "Hello, World!" << std::endl;
} else {
std::cout << "Goodbye, World!" << std::endl;
}
std::unique_ptr<Foo> _bar = FooFactory::getInstance()->createFoo("BAR");
if(_bar) {
std::cout << "Hello, World!" << std::endl;
} else {
std::cout << "Goodbye, World!" << std::endl;
}
I always see "Hello world!"
This makes me think that my use of the constructor is a std::unique_ptrbit offensive. Can someone give me a recommendation on how to approach this without emulating nullptrmyself ?
I am using gcc 4.4.6.