@@ -463,6 +463,80 @@ template void default_arg_dependent_context2<int>();
463463template void default_arg_dependent_context3<int >();
464464} // namespace default_arg
465465
466+ namespace default_init {
467+ template <class T >
468+ struct DepA {
469+ T arr[1 ];
470+ ~DepA () {}
471+ };
472+
473+ template <class T >
474+ struct DepB {
475+ int x;
476+ const DepA<T> &a = DepA<T>{{0 }};
477+ ~DepB () {}
478+ const int *begin () { return a.arr ; }
479+ const int *end () { return &a.arr [1 ]; }
480+ };
481+
482+ template <typename T>
483+ void default_init1_dependent () {
484+ // CHECK-CXX23: void @_ZN7P2718R012default_init23default_init1_dependentINS0_4DepBIiEEEEvv()
485+ // CHECK-CXX23-LABEL: for.cond.cleanup:
486+ // CHECK-CXX23-NEXT: call void @_ZN7P2718R012default_init4DepBIiED1Ev(
487+ // CHECK-CXX23-NEXT: call void @_ZN7P2718R012default_init4DepAIiED1Ev(
488+ for (auto &&x : T{0 }) {}
489+ }
490+
491+ template <typename T>
492+ void default_init2_dependent () {
493+ // CHECK-CXX23: void @_ZN7P2718R012default_init23default_init2_dependentINS0_4DepBIiEEEEvv()
494+ // CHECK-CXX23-LABEL: for.cond.cleanup:
495+ // CHECK-CXX23-NEXT: call void @_ZN7P2718R012default_init4DepBIiED1Ev(
496+ // CHECK-CXX23-NEXT: call void @_ZN7P2718R012default_init4DepAIiED1Ev(
497+ for (auto &&x : T{0 }.a .arr ) {}
498+ }
499+
500+ template void default_init1_dependent<DepB<int >>();
501+ template void default_init2_dependent<DepB<int >>();
502+ } // namespace default_init
503+
504+ // -- Examples from https://wg21.link/p2718r0
505+ extern void block_scope_begin_function ();
506+ extern void block_scope_end_function ();
507+ namespace std_examples {
508+ using T = std::list<int >;
509+ const T& f1 (const T& t) { return t; }
510+ const T& f2 (T t) { return t; }
511+ T g ();
512+ void foo () {
513+ // CHECK-CXX23: define {{.*}} void @_ZN7P2718R012std_examples3fooEv()
514+ // CHECK-CXX23: call void @_ZN7P2718R026block_scope_begin_functionEv
515+ block_scope_begin_function ();
516+ {
517+ // CHECK-CXX23-NEXT: call void @_ZN7P2718R012std_examples1gEv
518+ // CHECK-CXX23-NEXT: call {{.*}} @_ZN7P2718R012std_examples2f1ERKSt4listIiE
519+ // CHECK-CXX23: for.cond.cleanup:
520+ // CHECK-CXX23-NEXT: call void @_ZNSt4listIiED1Ev
521+ for (auto e : f1 (g ())) {} // OK, lifetime of return value of g() extended
522+ }
523+ // CHECK-CXX23: call void @_ZN7P2718R024block_scope_end_functionEv
524+ block_scope_end_function ();
525+
526+ // The lifetime of temporary returned by g() in this case will not be extended.
527+ // CHECK-CXX23: call void @_ZN7P2718R026block_scope_begin_functionEv
528+ block_scope_begin_function ();
529+ {
530+ // CHECK-CXX23-NEXT: call void @_ZN7P2718R012std_examples1gEv
531+ // CHECK-CXX23-NEXT: call {{.*}} @_ZN7P2718R012std_examples2f2ESt4listIiE
532+ // CHECK-CXX23-NEXT: call void @_ZNSt4listIiED1Ev
533+ for (auto e : f2 (g ())) {} // undefined behavior
534+ }
535+ // CHECK-CXX23: call void @_ZN7P2718R024block_scope_end_functionEv
536+ block_scope_end_function ();
537+ }
538+ } // namespace std_examples
539+
466540namespace basic {
467541using T = std::list<int >;
468542const T& f1 (const T& t) { return t; }
@@ -579,5 +653,51 @@ void default_arg3() {
579653 for (auto e : C (0 , C (0 , C (0 , C ())))) {}
580654}
581655} // namespace default_arg
582- } // namespace P2718R0
583656
657+ namespace default_init {
658+ struct X {
659+ int x;
660+ ~X () {}
661+ };
662+
663+ struct Y {
664+ int y;
665+ const X &x = X{1 };
666+ ~Y () {}
667+ };
668+
669+ struct A {
670+ int arr[1 ];
671+ const Y &y = Y{1 };
672+ ~A () {}
673+ };
674+
675+ struct B {
676+ int x;
677+ const A &a = A{{0 }};
678+ ~B () {}
679+ const int *begin () { return a.arr ; }
680+ const int *end () { return &a.arr [1 ]; }
681+ };
682+
683+ void default_init1 () {
684+ // CHECK-CXX23: void @_ZN7P2718R012default_init13default_init1Ev()
685+ // CHECK-CXX23-LABEL: for.cond.cleanup:
686+ // CHECK-CXX23-NEXT: call void @_ZN7P2718R012default_init1BD1Ev(
687+ // CHECK-CXX23-NEXT: call void @_ZN7P2718R012default_init1AD1Ev(
688+ // CHECK-CXX23-NEXT: call void @_ZN7P2718R012default_init1YD1Ev(
689+ // CHECK-CXX23-NEXT: call void @_ZN7P2718R012default_init1XD1Ev(
690+ for (auto &&x : B{0 }) {}
691+ }
692+
693+ void default_init2 () {
694+ // CHECK-CXX23: void @_ZN7P2718R012default_init13default_init2Ev()
695+ // CHECK-CXX23-LABEL: for.cond.cleanup:
696+ // CHECK-CXX23-NEXT: call void @_ZN7P2718R012default_init1BD1Ev(
697+ // CHECK-CXX23-NEXT: call void @_ZN7P2718R012default_init1AD1Ev(
698+ // CHECK-CXX23-NEXT: call void @_ZN7P2718R012default_init1YD1Ev(
699+ // CHECK-CXX23-NEXT: call void @_ZN7P2718R012default_init1XD1Ev(
700+ for (auto &&x : B{0 }.a .arr ) {}
701+ }
702+ } // namespace default_init
703+ } // namespace P2718R0
0 commit comments