2

I am a newbie in c++, and I suspect, that, of course the question relates not only to tuple.

So, I've watched a tutorial with roughly this code:

#include <tuple>

std::tuple<...> t(...)

Why #include <tuple>? Especially, given the fact that we explicitly write std::tuple. The code compiles without that #include line just well...

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Nurbol Alpysbayev
  • 19,522
  • 3
  • 54
  • 89
  • The `#include` provides the implementation (i.e. the tuple class). The `std::` is the namespace that the implementation resides in. Even if it works without the specifying the include explicitly, you should do so for clarity. – Mansoor Aug 21 '19 at 11:42
  • 3
    The code without the `#include` only compiles because the tuple got included through some other file – user1316208 Aug 21 '19 at 11:42
  • 1
    Always include the headers where a class or a function is defined or declared. Inter-dependencies between system headers (which system header include which other system headers) is an unknown implementation detail. – Some programmer dude Aug 21 '19 at 11:42
  • `#include` doesn't always provides implementation. It provides interface for non-template classes or functions ( in which case the implementation is provided by libraries and handled by linker), and implementation for template ones. – jerry_fuyi Aug 21 '19 at 11:48

4 Answers4

10

Because <tuple> is a header file which contains the tuple class inside the namespace std. Just because you're explicitliy saying std:: doesn't mean the compiler will just be able to find it if it's not included.

The reason it worked for you in this case is probably because another header you have included already includes <tuple> and thus your code includes <tuple> indirectly or because the compiler you're building with includes it automatically. This is not guaranteed by the standard and should not be relied upon. Always include the headers you'll need to make your code portable.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
9

You always should include the specific headers for any types that you use in your code.

The code compiles without that #include line just well...

That your code compiles without is just by chance, because it might have been included by another standard header you use in your program, and not guaranteed by the standard, and thus not portable.

Especially, given the fact that we explicitly write std::tuple.

The explicit use of the std:: namespace doesn't have any significance about that rule.

You should also always be explicit about using classes or types from the std namespace to prevent getting into ambiguity troubles.

Related stuff:

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
2

In C++ you need to include the header for everything you use. The std:: is about the namespace, it's completely different. Apparently your compiler is smart enough to deal with it, but most of the compilers won't.

Chelmy88
  • 1,106
  • 1
  • 6
  • 17
1

Some standard headers shall include other standard headers according to the C++ Standard. For example the header <algorithm> must include the header <initializer_list>.

However implementations are allowed to include standard headers in other standard headers at their discretion.

You should not rely on this because your program compiled with one compiler will not compile using another compiler.

Even if one standard header is included in another standard header according to the requirements of the C++ Standard nevertheless it is a good practice to include such a header explicitly because the user of the program (some other programmer) should bother about dependencies of the headers and will be free to include or exclude other headers.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335