Boost logo

Boost Users :

Subject: [Boost-users] class API return a const&, optional, shared_ptr
From: Hicham Mouline (hicham_at_[hidden])
Date: 2008-11-25 07:15:23


I have a class A that holds a container of objects of type B indexed by int
for e.g.

Typically, I have a function of A

class A {
    const B& getB(int <index> ) const;
   std::map< int, shared_ptr<B> > listOfBs_;

The implementation of getB could be

const B& A::getB(int <index> ) const
    //what to do if index not found

A number of solutions I think of:

boost::optional<const B&> getB(int <index> ) const

change signature to
const B* getB(int <index> ) const

Define a
static const B notFoundB;
const B& A::getB(int <index> ) const
returns notFoundB when not found and have API users compare return value
with notFoundB

I suppose the cheapest in overhead/performance is 2... optional must have
some time overhead?

Are there advised solutions? Favourites in terms of style?


Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at