Search code examples
c++header

Should I include <tuple>?


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...


Solution

  • 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.