Humam Helfawi Humam Helfawi - 2 months ago 7x
C++ Question

Protected variables naming and the standard

I came across this post where an iterable queue was introduced. The OP used a protected variable called

from the
in the implementation.

Is this totally valid? Would this variable have the same name over all implementations? In other words, does the standard state clearly that this variable must be named


For reference, the exact definition of std::queue is listed here.

So in answer to

In other words, does the standard state clearly that this variable must be named c?

Yes, it is in this case;

template <class T, class Container = deque<T>>
  class queue {
    Container c;
    // ...

In general, however, the names of the protected and private names and members are not standardised since the types are not all built to be derived from and the implementation is an implementation detail (and doesn't form part of the public API); e.g. std::vector doesn't list any protected names.

Some std containers and classes do define the names of protected members, the iostreams library comes to mind - basically the types that are intended to be derived from.

On a side note - do all the compilers/libraries use c? It appears as though at least the mainstream ones do (libstdc++, libc++ and MSVC). libstdc++ is interesting in that it includes the follow comment on the variable;

 *  'c' is the underlying container.  Maintainers wondering why
 *  this isn't uglified as per style guidelines should note that
 *  this name is specified in the standard, [].  (Why?
 *  Presumably for the same reason that it's protected instead
 *  of private: to allow derivation.  But none of the other
 *  containers allow for derivation.  Odd.)
_Sequence c;