I'm designing a library from scratch and want to get the public API as good as possible. I want the compiler to yell at me for misuse. Therefore, I've imposed the following rules on myself:
true (i.e. deep and complete) const correctness throughout the whole library
All things (local variables, member variables, member functions), which are not expected to change are declared
const
. That constness should propagate to all nested members and types.explicit and expressive ownership
In line with the C++ Core Guidelines I define that as (iff in the mathematical sense of if and only if):
- function arguments are
unique_ptr<T>
orT&&
iff the function is consuming it (i.e. taking ownership) - function arguments are
shared_ptr<const T>
orT const&
iff the function is only reading it - function arguments are
shared_ptr<T>
orT&
iff the function is modifying it without taking ownership - return values are
unique_ptr<T>
orT
iff the function transfers ownership to the caller - return values are
shared_ptr<const T>
orT const&
iff the caller should only read it (though the caller can construct a copy of it - givenT
is copyable) - no functions should return
shared_ptr<T>
,T&
orT*
(as it would allow for uncontrollable side effects, which I try to avoid by design)
- function arguments are
hidden implementation details
For now I'm going with abstract interfaces with factories returning the implementation as a
unique_ptr<Interface>
. Though, I'm open for alternative patterns which solve my problem described below.
I don't care about virtual table lookups and want to avoid dynamic casts by all means (I see them as a code smell).
Now, given two classes A
and B
, where B
owns a variable number of A
s. As well we've the B
-implementation BImpl
(implementation of A
is probably not of use here):
class A
{};
class B {
public:
virtual ~B() = default;
virtual void addA(std::unique_ptr<A> aObj) = 0;
virtual ??? aObjs() const = 0;
};
class BImpl : public B {
public:
virtual ~BImpl() = default;
void addA(std::unique_ptr<A> aObj) override;
??? aObjs() const override;
private:
std::vector<unique_ptr<A>> aObjs_;
};
I'm stuck with the return value of B
s getter to the vector of A
s: aObjs()
.
It should provide the list of A
s as read-only values without transfer of ownership (rule 2.5. above with const correctness) and still provide the caller easy access to all A
s, e.g. for use in range-based for
or standard algorithms such as std::find
.
I came up with the following options for those ???
:
std::vector<std::shared_ptr<const A>> const&
I would have to construct a new vector each time I call
aObjs()
(I could cache it inBImpl
). That feels not only inefficient and needlessly complex, but seems also very suboptimal.Replace
aObjs()
by a pair of functions (aObjsBegin()
andaObjsEnd()
) forwarding the constant iterator ofBImpl::aObjs_
.Wait. I would need to make that
unique_ptr<A>::const_iterator
aunique_ptr<const A>::const_iterator
to get my beloved const correctness. Again nasty casts or intermediate objects. And the user couldn't use it easily in range-basedfor
.
What obvious solution am I missing?
Edit:
B
should always be able to modify theA
s it is holding, thus declaringaObjs_
asvector<std::unique_ptr<const A>>
is not an option.Let
B
adhere to the iterator concept to iterate over theA
s, is neither an option asB
will hold a list ofC
s and a specificD
(or none).
std::vector<unique_ptr<const A>> aObjs_;
? – Howard Hinnant Aug 31 '16 at 19:17BImpl
should be able to modify theA
s it is owning. – Torbjörn Aug 31 '16 at 19:21