Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 747
Alek86 Avatar asked Oct 19 '25 08:10

Alek86


1 Answers

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(); }
like image 176
Barry Avatar answered Oct 21 '25 22:10

Barry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!