You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[SUGGESTION] Independent order and type data members #406
Independent order of declaration works for functions, because definitions of those functions put after declarations of variables.
However, declarations of types can intertwine with variables, so data members not always can refer those globals.
Code Example S: type = { mi: int = i; //cpp1 error - undeclared i pi: * int = i&; //cpp1 error - undeclared i
f: () = std::cout << i; //ok, function definition pushed down }
main: () = { li:= i; //ok, function definition pushed down std::cout << i; //ok }
i: int = 42;
Additional note
Probably hard to solve, requiring some sort of dependency graph
Namespaces doesn't seem to help here, because namespace itself still may be placed after class declaration.
In this case workaround is simple - move i: int = 42; declaration before S declaration
There's also no order independence in a type's Cpp1 declaration (template parameters and requires clause).
This fails because origin has not been declared (https://cpp2.godbolt.org/z/9WeKsef3j):
unit: @struct type = { }
quantity_point: @struct <O: * origin, U: * unit, N> type = { }
origin: @struct type = { }
main: () = { }
main.cpp2:2:10: error: unknown type name 'origin'
2 | template<origin* O, unit* U, typename N> class quantity_point;
| ^
Phase 3 "Cpp2 function definitions"
then emits, in source order:
Of a Cpp2 type, its member function definitions.
Of a Cpp2 function, its Cpp1 definition.
That results in these order dependencies:
The body of a function is order independent.
I think Cpp2 guarantees this.
Anything else can be source code order dependent according to how things are emitted above.
Notably, a function with a deduced return type can't be used before it's defined.
E.g., if A and B are Cpp2 types,
you can use the incomplete type A in type B's template parameter list or requires clause
if A comes before B in source code.
@hsutter What do you think about concepts and order independence?
Right now, a concept is emitted during Phase 2 "Cpp2 type definitions and function declarations".
But that's no good if you want to use it to constrain template types in the same TU.
Repository owner locked and limited conversation to collaborators on Aug 30, 2023
Activity
JohelEGP commentedon Jul 31, 2023
There's also no order independence in a type's Cpp1 declaration (template parameters and requires clause).
This fails because
originhas not been declared (https://cpp2.godbolt.org/z/9WeKsef3j):JohelEGP commentedon Jul 31, 2023
Here is the order of emitted Cpp1
(template implied where possible):
first emits
#include "cpp2util.h".then emits, in source order:
then emits, in source order:
That includes
#includedirectives, even if for a Cpp2 source file (see [BUG] Can't use#included declaration in requires clause #470).then emits, in source order:
That results in these order dependencies:
I think Cpp2 guarantees this.
Notably, a function with a deduced return type can't be used before it's defined.
E.g., if
AandBare Cpp2 types,you can use the incomplete type
Ain typeB's template parameter list or requires clauseif
Acomes beforeBin source code.JohelEGP commentedon Aug 10, 2023
See the last paragraph of #75 (comment).
JohelEGP commentedon Aug 11, 2023
@hsutter What do you think about
concepts and order independence?Right now, a
conceptis emitted during Phase 2 "Cpp2 type definitions and function declarations".But that's no good if you want to use it to constrain template types in the same TU.