在 2022/12/2 15:18, leon zadorin via Gcc-help 写道: > (1) On the one hand, given that I do instantiate ::std::optional with S > (whith has member v whose elements' types, I guess(?) are at the line of > declaring 'static ::std::optional s', are still incompletely denifed, > i.e. X) -- the clang discussion appears to say that at this moment optional > is instantiated and S is incomplete (?) ... as so it is a UB... which I > understood Jonathan's comment to relate to as well. > Yes this requires `S` to be complete. > (2) However, from your comment it would appear that 'instantiation of > members of class template' is done afterwards anyways... > Only `S` itself is required to be complete. In your code: struct S { ::std::vector<::std::shared_ptr>> v; }; `S` _is_ complete. It does not matter whether it contains a `vector` of or a `shared_ptr` to incomplete types, because the standard allows so. ([vector.overview] 4, [util.smartptr.shared] 2) > I suppose to reconcile both bits of info: > > (a) 'instantitation of members of class template' ... is it the same for > instantitaion of the class template itself? (because UB specs appears to > provide restrictions on std lib class instantiations, not specifically to > inner members, but just for whole class)?; and > No. ``` struct T; extern T* eptr; // okay; pointer to incomplete type struct S { shared_ptr mptr; // okay; `shared_ptr` to incomplete type }; void use_s() { S s; // oayk; `S` is complete s.mptr.reset(); // okay; `reset()` does not require `T` to // be complete s.mptr.reset(::eptr); // error; instantiates the default // deleter expression which requires // `T` to be complete } ``` > (b) in the above code right at the line 'static ::std::optional s' is S > considered incomplete type at that moment? > No. See above. > ... I'm just trying to figure out whether template "::std::optional" is, > itself, being instantiated at that very line (never mind its members) and > if type S at that moment is considered to be incomplete... in which case it > is UB... if not then may be its not a UB... I am confused :) ha ha :) > wouldn't be the first time though :) If `shared_ptr` is a bad example for you, here is a much simpler one: ``` struct incomplete; struct complete { incomplete* ptr; // okay incomplete& ref; // also okay incomplete val; // invalid use of incomplete type }; ``` -- Best regards, LIU Hao