styleguide icon indicating copy to clipboard operation
styleguide copied to clipboard

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

Open marcogmaia opened this issue 1 year ago • 0 comments

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.h in c.cpp even 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.

marcogmaia avatar Nov 13 '24 16:11 marcogmaia