The C++ standard library headers may include each other in unspecified ways, so programmers generally shouldn't depend on one header including another. In a few cases, however, a header is guaranteed to include another header, or make available certain functions that would otherwise require inclusion of another header. What are those cases?
This answer ignores C headers - both the <meow.h> and <cmeow> ones. Of the C++ library headers (all references are to N4659):
<initializer_list> is guaranteed to be included by:
<utility> (§23.2.1 [utility.syn])
<string> (§24.3.1 [string.syn])
<array> (§26.3.2 [array.syn])
<deque> (§26.3.3 [deque.syn])
<forward_list> (§26.3.4 [forward_list.syn])
<list> (§26.3.5 [list.syn])
<vector> (§26.3.6 [vector.syn])
<map> (§26.4.2 [associative.map.syn])
<set> (§26.4.3 [associative.set.syn])
<unordered_map> (§26.5.2 [unord.map.syn])
<unordered_set> (§26.5.3 [unord.set.syn])
<queue> (§26.6.2 [queue.syn])
<stack> (§26.6.3 [stack.syn])
<algorithm> (§28.2 [algorithms.syn])
<random> (§29.6.2 [rand.synopsis])
<valarray> (§29.7.1 [valarray.syn])
<regex> (§31.4 [re.syn])
<iostream> is guaranteed to include <ios>, <streambuf>, <istream>, and <ostream> (§30.4.1 [iostream.syn]).
<ios> is guaranteed to include <iosfwd> (§30.5.1 [ios.syn]).
<bitset> is guaranteed to include <string> and <iosfwd> (§23.9.1 [bitset.syn]).
The free function templates std::begin, std::end, the C++14 c-, r-, and cr- versions, and the C++17 free function templates std::size, std::empty and std::data nominally reside in <iterator>, but are also available if any of the following headers is included: <array>, <deque>, <forward_list>, <list>, <map>, <regex>, <set>, <string>, <unordered_map>, <unordered_set>, and <vector> (§27.7 [iterator.range], §27.8 [iterator.container]).
When <string_view> is included, the *begin and *end functions, and the two generic std::swap overloads defined in [utility.swap] (swap(T&, T&) and swap(T (&a)[N], T (&b)[N])) are guaranteed to be available. size/empty/data, however, are not. (§24.4.1 [string.view.synop]).
Here are the mandatory includes for C++20, taken from N4860.
compare is included in:
array
chrono
coroutine
deque
filesystem
forward_list
iterator
list
map
memory
optional
queue
ranges
regex
set
stack
string
string_view
system_error
thread
tuple
typeindex
unordered_map
unordered_set
utility
variant
vector
initializer_list is included in:
algorithm
array
deque
forward_list
list
map
queue
random
ranges
regex
set
stack
string
thread
unordered_map
unordered_set
utility
valarray
vector
string is included in:
bitset
iosfwd is included in:
bitset
ios
concepts is included in:
iterator
iterator is included in:
ranges
ios, streambuf, istream are included in:
iostream
ostream is included in:
iostream
syncstream
cinttypes is included in:
cstdint
Related
This question already has answers here:
Which headers in the C++ standard library are guaranteed to include another header?
(2 answers)
Closed 1 year ago.
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;
}
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.
I want to avoid this case:
h1.hpp:
#include <vector>
//some code
main.cpp:
#include "h1.hpp"
//#include <vector> This include is commented.
int main(){
std::vector<int> x; //works fine!
}
I want to get an error/warning that I am using <vector> while it is not directly included in MSVS.
If <vector> is used as an implementation detail (i.e., function body), separate h1.hpp to h1_head.hpp (declarations) and h1_body.hpp (definitions). If you have structs / classes, you can hide the details by e.g. declaring a detail struct / class inside your class and only defining it in h1_body.hpp . Include h1_head.hpp before the code in main.cpp and h2_body.hpp after all the codes.
If any of the fuctions in h1.hpp take / return a vector or a related type, then maybe it's best to assume that main.cpp will have <vector> when including h1. An alternative to this is to say that h1 requires a vector class and thus not include <vector> in h1 but require it to be included in main.cpp (before h1) - this is, however very confusing as there's no programmatic way of documenting it (no, requiring a typedef std::vector vector_t; or a #define H1VECTOR std::vector is not documenting, it's false generalization).
The C++ standard library headers may include each other in unspecified ways, so programmers generally shouldn't depend on one header including another. In a few cases, however, a header is guaranteed to include another header, or make available certain functions that would otherwise require inclusion of another header. What are those cases?
This answer ignores C headers - both the <meow.h> and <cmeow> ones. Of the C++ library headers (all references are to N4659):
<initializer_list> is guaranteed to be included by:
<utility> (§23.2.1 [utility.syn])
<string> (§24.3.1 [string.syn])
<array> (§26.3.2 [array.syn])
<deque> (§26.3.3 [deque.syn])
<forward_list> (§26.3.4 [forward_list.syn])
<list> (§26.3.5 [list.syn])
<vector> (§26.3.6 [vector.syn])
<map> (§26.4.2 [associative.map.syn])
<set> (§26.4.3 [associative.set.syn])
<unordered_map> (§26.5.2 [unord.map.syn])
<unordered_set> (§26.5.3 [unord.set.syn])
<queue> (§26.6.2 [queue.syn])
<stack> (§26.6.3 [stack.syn])
<algorithm> (§28.2 [algorithms.syn])
<random> (§29.6.2 [rand.synopsis])
<valarray> (§29.7.1 [valarray.syn])
<regex> (§31.4 [re.syn])
<iostream> is guaranteed to include <ios>, <streambuf>, <istream>, and <ostream> (§30.4.1 [iostream.syn]).
<ios> is guaranteed to include <iosfwd> (§30.5.1 [ios.syn]).
<bitset> is guaranteed to include <string> and <iosfwd> (§23.9.1 [bitset.syn]).
The free function templates std::begin, std::end, the C++14 c-, r-, and cr- versions, and the C++17 free function templates std::size, std::empty and std::data nominally reside in <iterator>, but are also available if any of the following headers is included: <array>, <deque>, <forward_list>, <list>, <map>, <regex>, <set>, <string>, <unordered_map>, <unordered_set>, and <vector> (§27.7 [iterator.range], §27.8 [iterator.container]).
When <string_view> is included, the *begin and *end functions, and the two generic std::swap overloads defined in [utility.swap] (swap(T&, T&) and swap(T (&a)[N], T (&b)[N])) are guaranteed to be available. size/empty/data, however, are not. (§24.4.1 [string.view.synop]).
Here are the mandatory includes for C++20, taken from N4860.
compare is included in:
array
chrono
coroutine
deque
filesystem
forward_list
iterator
list
map
memory
optional
queue
ranges
regex
set
stack
string
string_view
system_error
thread
tuple
typeindex
unordered_map
unordered_set
utility
variant
vector
initializer_list is included in:
algorithm
array
deque
forward_list
list
map
queue
random
ranges
regex
set
stack
string
thread
unordered_map
unordered_set
utility
valarray
vector
string is included in:
bitset
iosfwd is included in:
bitset
ios
concepts is included in:
iterator
iterator is included in:
ranges
ios, streambuf, istream are included in:
iostream
ostream is included in:
iostream
syncstream
cinttypes is included in:
cstdint
Sorry. I'm somewhat new to building larger programs and linking files together and all that good stuff.
My function declarations in my header .h file have std::vector in them. Those function implementations in my header .cpp file also use std::vector. From my understanding, I should just use the "#include " in my header .h file because everything from that file will essentially be pasted into my header .cpp file at compile time. I'm just trying to make sure.
My .cpp file:
#include "PlagiarismDetector_Header.h"
// implementations of functions defined in PlagiarismDetector_Header.h
PlagiarismDetector_Header.h:
#include <iostream> // std::cout, std::endl
#include <vector> // std::vector
#include <string> // std::string
#include <algorithm> // std::swap
#include <math.h> // sqrt()
#include <set> // std::set
#include <map> // std::map
#include <fstream> // std::ifstream
std::vector<std::vector<std::string> > get_file_sntncs(std::fstream&);
std::vector<std::string> get_sntnc_wrds(const std::string&);
double sntnc_smlrty_qtnt(std::vector<std::string>, std::vector<std::string>);
// All the characters that constitute a word
const char LETTERS_ARR[] = {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'."};
const std::set<char> LETTERS_SET(LETTERS_ARR, LETTERS_ARR + sizeof(LETTERS_ARR)/sizeof(char));
You got it pretty much right but consider these aspects:
try to put an include guard in your header file since you may need to have it included twice in row in a future file
consider to push down to the implementation file(s) all constants and functions declarations that do not need to be exposed to the extern modules (whichever file including your header will see them even tough it does not need to use them)
if you use those constants in other files (that will include your header), follow john suggestion and make them extern
I have a problem that does no resurface (even no warnings) in XCode but does allow me to compile in Keil MDK.
void grammar::parse(std::string &_expr) {
std::transform(_expr.begin(), _expr.end(), _expr.begin(), std::tolower);
_expr.erase(std::remove_if(_expr.begin(), _expr.end(), std::isspace), _expr.end());
}
That is what I get
error: #304: no instance of overloaded function "std::transform" matches the argument list
error: #304: no instance of function template "std::remove_if" matches the argument list
Header included:
#include <iostream>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <stdio.h>
#include <algorithm>
Could you please advise me on where to look? I am surprised that XCode version works as expected...
You include ctype.h, that header declares a function tolower in the global namespace (this is part of the C library, so there are no other namespaces there). Maybe you meant to include cctype. For a given C standard library header X.h, there is a c++ version cX that provides some of the same functionality inside the ::std namespace.