45

All the names in the standard C++ library are lowercase except std::ios_base::Init. Why is this?

T.C.
  • 133,968
  • 17
  • 288
  • 421
template boy
  • 10,230
  • 8
  • 61
  • 97
  • I'd say it's simply because it does a lot of important initialization, maintenance and cleanup work for the IO streams. It's unique in that regard, so it got a capital. – IVlad Mar 09 '15 at 21:48
  • 1
    Backwards compatibility. – Alan Stokes Mar 09 '15 at 22:01
  • 2
    Because the naming conventions and indeed the names in C++ are a complete shambles, just as they were in C. – user207421 Mar 09 '15 at 22:13
  • 1
    Perhaps that library was programmed by a programmer who "knew better" and didn't want to follow the convention. I see this fairly often in the industry. – Sildoreth Mar 25 '15 at 22:22

1 Answers1

26

In an early draft of the IOStream classes you can see that ios_base was originally not present, basic_ios was the base class and was responsible for Init and other members which were later separated into ios_base.

basic_ios also had (and still has) an init function, and while the two names did not necessarily have to be distinct, presumably to try and avoid confusion it was decided to name the class Init and the function init. Also, without this distinction something like the following would have been necessary when using the class:

struct ios_base
{
  struct init{};
  void init();
};

ios_base::init i1;        // error
struct ios_base::init i2; // ok

The idea of having a separate class responsible for initializing the standard streams goes back to at least CFront 3.0, as can be seen by Iostream_init in this header. At some point it was decided not only to make the class a member of ios/ basic_ios / ios_base but that it should be renamed Init.

In any case it makes sense to disambiguate the two, even considering the fact that they were ultimately placed in separate classes. They could have chosen completely separate names instead, but presumably Init / init was considered as the best semantic choice to describe what the class and function do, and there's also a chance it was simply mimicking the older Iostream_init, which for some reason also starts with an uppercase I.

user657267
  • 20,568
  • 5
  • 58
  • 77
  • 1
    "has to" is too strong, but yes, a member function with the same name as the base class's member class would cause that member class to be hidden. –  Mar 09 '15 at 22:03
  • 2
    Hmm... Inspecting pre-standard libstdc++ implementations, in the evolution that led to the C++98 standard, there was a point where there was just an `ios` class containing both `Init` and `init`. At that point, the different names were absolutely necessary, not just nice to have. –  Mar 09 '15 at 22:15
  • 1
    @hvd I've reworded the answer thanks to your comment, the init / Init distinction indeed seems to be an artefact from the distant past. I'd like to point out though that a function and a class with the same name can still happily coexist in the same scope. – user657267 Mar 10 '15 at 04:37
  • @user657267 Right, somehow I missed that. It would be inconvenient, but it still wouldn't be *required* to use different names. –  Mar 10 '15 at 06:48