Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are iostream sentry objects called "cerberos" in the standard?

I was wondering what __cerb means in the libstdc++ of g++, and I found out that this seems to be a short name for cerberos as named in the official C++ standard document.

§ 22.3.1 / 3:

[ Example: An iostream operator<< might be implemented as:

template <class charT, class traits>
basic_ostream<charT,traits>&
operator<< (basic_ostream<charT,traits>& s, Date d) {
                                                            //      !!!!!!!!!!
  typename basic_ostream<charT,traits>::sentry cerberos(s); // <--- !! HERE !!
                                                            //      !!!!!!!!!!
  if (cerberos) {
    ios_base::iostate err = ios_base::iostate::goodbit;
    tm tmbuf; d.extract(tmbuf);
    use_facet<time_put<charT,ostreambuf_iterator<charT,traits> > >(
      s.getloc()).put(s, s, s.fill(), err, &tmbuf, ’x’);
    s.setstate(err);            // might throw
  }
  return s;
}

— end example ]

Why are iostream sentry objects called cerberos, and what does cerberos actually mean?

like image 589
helami Avatar asked Mar 01 '13 23:03

helami


1 Answers

Cerberus was the three headed dog1 who guarded the gates of hell, keeping those trapped within from crossing back over the river Styx. Whomever wrote that section fancied Greek and/or Roman mythology and decided it would be an applicable name for a local variable sentry.

As to why it ends in os, one can imagine three scenarios:

  1. They completely forgot the rules for Greek and Latin noun declinations and thought that was how it is spelled
  2. I completely forgot the rules for Greek and Latin noun declinations and am pointing out a non-issue.
  3. They conflated the spellings of Cerberus and Kerberos, giving birth to Cerberos the basic_stream sentry of Greek and Roman descent.

1. Apparently the number of heads on Cerberus is variable. Perhaps this is better expressed as the "N-headed dog, where N is greater than or equal to 1."

like image 81
user7116 Avatar answered Oct 31 '22 17:10

user7116