Search code examples
c++modulefunction-declarationc++20

C++20 modules TS: still need function declaration?


I hope a quick small question to those who tried C++20 modules

According to TS, should this code compile in C++20?

void f1() { f2(); }
void f2() { ... }

For example, in C++11 it won't compile, because f1() doesn't "know" about the f2(), f2() must be declared before usage.

But maybe in C++20 this requirement will be eliminated in modules?

If the first code snippet is not compiled, will this one compile

void f1() { f2(); }
export void f2() { ... }

because f2() will be seen from the BMI?


Solution

  • While Modules does change many things about name lookup (the latest paper I'm aware of is P1103R1), Modules will not change the fundamental property of C++ that names must be declared before use (modulo things like dependent unqualified calls - which simply delays the lookup, but it still has to actually happen).

    This:

    void f1() { f2(); }
    export void f2() { ... }
    

    will still be a compile error if there is no previous declaration of f2 or it wasn't imported from somewhere. You'll have to write:

    export void f2() { ... }
    void f1() { f2(); }