struct A; void check(A const&) { /* */ } struct A { A() { check(*this); } };
我的意思是,我正在调用和外部功能与一个尚未活着的对象.这是不明确的行为吗?
相关问题:如果我把这个检查功能作为一个成员函数(静态或非静态的),那么这个标准对于在构造函数之外但是在类内部使用非活动对象呢?
在课堂观点与用户之间(一类课外与课外生活)有什么不同的终身概念?
解决方法
The lifetime of an object of type
T
begins when:
- storage with the proper alignment and size for type
T
is obtained,and- if the object has non-vacuous initialization,its initialization is complete.
A具有非空的初始化.它的初始化完成时,从[class.base.init] / 13:
In a non-delegating constructor,initialization proceeds in the following order:
- …
- — Finally,the compound-statement of the constructor body is executed.
然而,尽管一开始还没有开始,该标准还在[class.base.init] / 16:
Member functions (including virtual member functions,10.3) can be called for an object under construction… However,if these operations are performed in a ctor-initializer (or in a function called directly
or indirectly from a ctor-initializer) before all the mem-initializers for base classes have completed,the result of the operation is undefined.
关于一生的问题,没有区别:
void check(const A& ) { .. } struct A { A() { check(*this); } };
和:
struct A { void check() const { .. } A() { check(); } };
后者被明确允许(因为它不在一个ctor-initializer中),所以我没有理由以终身理由排除前者.