google / styleguide

Style guides for Google-originated open-source projects
https://google.github.io/styleguide/
Apache License 2.0
37.53k stars 13.3k forks source link

[C++][IWYU] How to treat includes in case of compiler deduction? #863

Open marcogmaia opened 1 week ago

marcogmaia commented 1 week ago

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:

I'd appreciate insights on this scenario.