styleguide
styleguide copied to clipboard
[C++][IWYU] How to treat includes in case of compiler deduction?
I have a question about header inclusion best practices when dealing with implicit type construction.
Consider the following code structure:
// a.h
struct A { int x, y; };
// b.h
#include "a.h"
struct B { void Func(A a) { /* use a */ } };
// c.cpp
#include "b.h"
void Func() {
B b;
b.Func({1, 2}); // Implicit construction of A
}
In c.cpp, we're using struct A implicitly through brace initialization, and the compiler successfully deduces the type for us because b.h includes a.h. This pattern is common when working with aggregate types.
Questions:
- Should we explicitly include
a.hinc.cppeven though we're not directly referencing the symbol? - What's the general consensus for header inclusion when dealing with implicit type construction?
I'd appreciate insights on this scenario.