0

I have inherited a C++ project that was written in 2014 and indeed compiles with GCC 4.8. In a particular file, several classes currently found in the <random> header of C++ standard library are instantiated. These include mt19937, random_device, uniform_real_distribution, and normal_distribution.

When I compile this file with GCC 7.3, I get an error saying that these classes are not defined in the std namespace. This error (obviously) goes away when I include <random>.

My question is why this error does not happen in GCC 4.8? Were these classes previously found under a different header?

Adam Sperry
  • 283
  • 1
  • 9
  • 3
    Pure luck. The `` header could have been included somewhere else by the older implementation. If you need classes defined in a specific header, always explicitly include that header. – Some programmer dude Nov 13 '18 at 05:23
  • 1
    There are no guarantees of what included by another header. With some library implementations you'll find by including `` you don't have to include ``. In others, no such luck. The general rule of thumb is to always include all of the headers you need if for no better reason than so you don't get a nasty surprise after upgrading your tools. – user4581301 Nov 13 '18 at 05:27
  • I'm definitely on the boat when it comes to including what you use in every file. This code was inherited. These answers make sense. Thank you! So I'm correct in my understanding that these classes have been in `` since C++11 (and nowhere else)? And `` may not have been necessary in 4.8 because it was probably indirectly included by another standard library header, but in 7.3 this is not the case? – Adam Sperry Nov 13 '18 at 05:38
  • @AdamSperry: Correct. There are quite a few similar questions, but none specifically about `` in GCC4.8. I'll copy the comment to an answer, to we can mark this question as solved. – MSalters Nov 13 '18 at 08:30

1 Answers1

3

Standard headers may include other headers. This can be useful when they share an implementation. However, these things can change over time, e.g. when the common parts are refactored to a third (internal) header file.

Since we don't know exactly how <random> got included indirectly with GCC4.8, we can't be absolutely positive about what happened, but it is not surprising.

MSalters
  • 173,980
  • 10
  • 155
  • 350