1

I am looking at the example given for std::tolower in c++ reference and it seems the method std::tolower works even if I replace library <locale> with <algorithm>

Is <locale> imported indirectly if <algorithm> is included?

Example:

// tolower example (C++)
#include <iostream> 
#include <string>
#include <locale>         // <-- works for std::locale, std::tolower
#include <algorithm>      // <-- also works for std::tolower

int main ()
{
  std::locale loc;
  std::string str="Test String.\n";
  for (std::string::size_type i=0; i<str.length(); ++i)
    std::cout << std::tolower(str[i],loc);
  return 0;
}
mathworker
  • 171
  • 13
  • 1
    The implementation of `` from your compiler may or may not include ``. Even if it does today, it may or may not tomorrow, and it may or may not during leap years. Don't rely on undocumented, unspecified behavior. – Brian61354270 Apr 05 '21 at 15:45

1 Answers1

1

Is <locale> imported indirectly if <algorithm> is included?

There is no import (before C++20 and modules) in C++11, just includes. Read n3337 (or some newer C++ standard), the documentation of your compiler (e.g. GCC) and preprocessor (e.g. GNU cpp).

If you use GCC, compile your C++ code mathworker.cc as g++ -Wall -Wextra -g -H -c mathworker.cc and you will see what files are #include-d.

On some implementations it could happen that <locale> gets included.

But if you care about portability to other C++ compilers (like Clang, or a newer version of GCC) you should code explicitly the #includes documented in e.g. this C++ reference.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547