Is there a way to make member function NOT callable from constructor? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceWhat are the differences between a pointer variable and a reference variable in C++?Virtual member call in a constructorHow do I call one constructor from another in Java?Can I call a constructor from another constructor (do constructor chaining) in C++?What is a clean, pythonic way to have multiple constructors in Python?Throwing exceptions from constructorsCall one constructor from anotherEquality-compare std::weak_ptrClass inheritance: Constructor and member functions of class not recognized by compilerIs it bad practice to have a constructor function return a Promise?
Two different pronunciation of "понял"
Who can trigger ship-wide alerts in Star Trek?
3 doors, three guards, one stone
How can I make names more distinctive without making them longer?
What items from the Roman-age tech-level could be used to deter all creatures from entering a small area?
What do you call a plan that's an alternative plan in case your initial plan fails?
Active filter with series inductor and resistor - do these exist?
If I can make up priors, why can't I make up posteriors?
Limit for e and 1/e
How to politely respond to generic emails requesting a PhD/job in my lab? Without wasting too much time
Can smartphones with the same camera sensor have different image quality?
I'm having difficulty getting my players to do stuff in a sandbox campaign
Why don't the Weasley twins use magic outside of school if the Trace can only find the location of spells cast?
Why is "Captain Marvel" translated as male in Portugal?
Windows 10: How to Lock (not sleep) laptop on lid close?
Why use gamma over alpha radiation?
Slither Like a Snake
Writing Thesis: Copying from published papers
Problem when applying foreach loop
Working around an AWS network ACL rule limit
Biased dice probability question
What LEGO pieces have "real-world" functionality?
Stars Make Stars
When communicating altitude with a '9' in it, should it be pronounced "nine hundred" or "niner hundred"?
Is there a way to make member function NOT callable from constructor?
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceWhat are the differences between a pointer variable and a reference variable in C++?Virtual member call in a constructorHow do I call one constructor from another in Java?Can I call a constructor from another constructor (do constructor chaining) in C++?What is a clean, pythonic way to have multiple constructors in Python?Throwing exceptions from constructorsCall one constructor from anotherEquality-compare std::weak_ptrClass inheritance: Constructor and member functions of class not recognized by compilerIs it bad practice to have a constructor function return a Promise?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have member function (method) which uses
std::enable_shared_from_this::weak_from_this()
In short: weak_from_this
returns weak_ptr
to this. One caveat is it can't be used from constructor.
If somebody would use my function from constructor of inherited class, weak_from_this
inside it would return expired weak_ptr
. I guard against that with assertion checking that it's not expired, but it's a run-time check.
Is there a way to check against it at compile time?
c++ constructor c++17 shared-ptr weak-ptr
add a comment |
I have member function (method) which uses
std::enable_shared_from_this::weak_from_this()
In short: weak_from_this
returns weak_ptr
to this. One caveat is it can't be used from constructor.
If somebody would use my function from constructor of inherited class, weak_from_this
inside it would return expired weak_ptr
. I guard against that with assertion checking that it's not expired, but it's a run-time check.
Is there a way to check against it at compile time?
c++ constructor c++17 shared-ptr weak-ptr
Note there is a difference in scope between a child class constructor body and the parent class constructor: the latter has been executed completely before you even start initializing the child class's members (if any), let alone enter the child class constructor body.
– rubenvb
Apr 8 at 14:58
4
Nice question. One way would be to make a dummy class with pure virtual functionweak_from_this
and inherit yours from it. This will make it a hard compile error.
– SergeyA
Apr 8 at 14:59
5
@SergeyA Why didn't you post that as an answer? All other people here seem to conclude that it's not possible so either your comment is wrong and misleading or they are wrong and you should show how it can be achieved.
– Bakuriu
Apr 8 at 21:17
@Bakuriu well, I did not have the energy to polish it to the full blown answer. It is possible that it is not a workable solution.
– SergeyA
Apr 9 at 14:07
Simply based on the fact the static type of an object doesn't (and cannot) depend on the fact its construction is completed. Note that if that were the case, you wouldn't be able in a ctor ofT
to storethis
in a global obj register, and use the obj later, w/o a cast, as the ptr stored would have "(in construction)T*" not plainT*
.
– curiousguy
13 hours ago
add a comment |
I have member function (method) which uses
std::enable_shared_from_this::weak_from_this()
In short: weak_from_this
returns weak_ptr
to this. One caveat is it can't be used from constructor.
If somebody would use my function from constructor of inherited class, weak_from_this
inside it would return expired weak_ptr
. I guard against that with assertion checking that it's not expired, but it's a run-time check.
Is there a way to check against it at compile time?
c++ constructor c++17 shared-ptr weak-ptr
I have member function (method) which uses
std::enable_shared_from_this::weak_from_this()
In short: weak_from_this
returns weak_ptr
to this. One caveat is it can't be used from constructor.
If somebody would use my function from constructor of inherited class, weak_from_this
inside it would return expired weak_ptr
. I guard against that with assertion checking that it's not expired, but it's a run-time check.
Is there a way to check against it at compile time?
c++ constructor c++17 shared-ptr weak-ptr
c++ constructor c++17 shared-ptr weak-ptr
edited 13 hours ago
curiousguy
4,68823046
4,68823046
asked Apr 8 at 14:53
KorriKorri
37629
37629
Note there is a difference in scope between a child class constructor body and the parent class constructor: the latter has been executed completely before you even start initializing the child class's members (if any), let alone enter the child class constructor body.
– rubenvb
Apr 8 at 14:58
4
Nice question. One way would be to make a dummy class with pure virtual functionweak_from_this
and inherit yours from it. This will make it a hard compile error.
– SergeyA
Apr 8 at 14:59
5
@SergeyA Why didn't you post that as an answer? All other people here seem to conclude that it's not possible so either your comment is wrong and misleading or they are wrong and you should show how it can be achieved.
– Bakuriu
Apr 8 at 21:17
@Bakuriu well, I did not have the energy to polish it to the full blown answer. It is possible that it is not a workable solution.
– SergeyA
Apr 9 at 14:07
Simply based on the fact the static type of an object doesn't (and cannot) depend on the fact its construction is completed. Note that if that were the case, you wouldn't be able in a ctor ofT
to storethis
in a global obj register, and use the obj later, w/o a cast, as the ptr stored would have "(in construction)T*" not plainT*
.
– curiousguy
13 hours ago
add a comment |
Note there is a difference in scope between a child class constructor body and the parent class constructor: the latter has been executed completely before you even start initializing the child class's members (if any), let alone enter the child class constructor body.
– rubenvb
Apr 8 at 14:58
4
Nice question. One way would be to make a dummy class with pure virtual functionweak_from_this
and inherit yours from it. This will make it a hard compile error.
– SergeyA
Apr 8 at 14:59
5
@SergeyA Why didn't you post that as an answer? All other people here seem to conclude that it's not possible so either your comment is wrong and misleading or they are wrong and you should show how it can be achieved.
– Bakuriu
Apr 8 at 21:17
@Bakuriu well, I did not have the energy to polish it to the full blown answer. It is possible that it is not a workable solution.
– SergeyA
Apr 9 at 14:07
Simply based on the fact the static type of an object doesn't (and cannot) depend on the fact its construction is completed. Note that if that were the case, you wouldn't be able in a ctor ofT
to storethis
in a global obj register, and use the obj later, w/o a cast, as the ptr stored would have "(in construction)T*" not plainT*
.
– curiousguy
13 hours ago
Note there is a difference in scope between a child class constructor body and the parent class constructor: the latter has been executed completely before you even start initializing the child class's members (if any), let alone enter the child class constructor body.
– rubenvb
Apr 8 at 14:58
Note there is a difference in scope between a child class constructor body and the parent class constructor: the latter has been executed completely before you even start initializing the child class's members (if any), let alone enter the child class constructor body.
– rubenvb
Apr 8 at 14:58
4
4
Nice question. One way would be to make a dummy class with pure virtual function
weak_from_this
and inherit yours from it. This will make it a hard compile error.– SergeyA
Apr 8 at 14:59
Nice question. One way would be to make a dummy class with pure virtual function
weak_from_this
and inherit yours from it. This will make it a hard compile error.– SergeyA
Apr 8 at 14:59
5
5
@SergeyA Why didn't you post that as an answer? All other people here seem to conclude that it's not possible so either your comment is wrong and misleading or they are wrong and you should show how it can be achieved.
– Bakuriu
Apr 8 at 21:17
@SergeyA Why didn't you post that as an answer? All other people here seem to conclude that it's not possible so either your comment is wrong and misleading or they are wrong and you should show how it can be achieved.
– Bakuriu
Apr 8 at 21:17
@Bakuriu well, I did not have the energy to polish it to the full blown answer. It is possible that it is not a workable solution.
– SergeyA
Apr 9 at 14:07
@Bakuriu well, I did not have the energy to polish it to the full blown answer. It is possible that it is not a workable solution.
– SergeyA
Apr 9 at 14:07
Simply based on the fact the static type of an object doesn't (and cannot) depend on the fact its construction is completed. Note that if that were the case, you wouldn't be able in a ctor of
T
to store this
in a global obj register, and use the obj later, w/o a cast, as the ptr stored would have "(in construction)T*" not plain T*
.– curiousguy
13 hours ago
Simply based on the fact the static type of an object doesn't (and cannot) depend on the fact its construction is completed. Note that if that were the case, you wouldn't be able in a ctor of
T
to store this
in a global obj register, and use the obj later, w/o a cast, as the ptr stored would have "(in construction)T*" not plain T*
.– curiousguy
13 hours ago
add a comment |
3 Answers
3
active
oldest
votes
I am afraid the answer is "no, it's not possible to protect against this at compile-time." It's always difficult to prove a negative, but consider this: if it was possible to protect a function this way, it would probably have been done for weak_from_this
and shared_from_this
in the standard library itself.
add a comment |
No there is no way. Consider:
void call_me(struct widget*);
struct widget : std::enable_shared_from_this<widget>
widget()
call_me(this);
void display()
shared_from_this();
;
// later:
void call_me(widget* w)
w->display(); // crash
The thing is there is a reason you want to check for not calling shared_from_this
in the constructor. Think about that reason. It's not that shared_from_this
cannot be called, it's because it's return value has no way of being assigned yet. It is also not because it will never be assigned. It's because it will be assigned later in the execution of the code. Order of operation is a runtime property of your program. You cannot assert at compile time for order of operation, which is done at runtime.
add a comment |
Not as such, but - if performance is not an issue, you could add a flag which indicates construction is complete, and use that to fail at run-time with such calls:
class A
// ... whatever ...
public:
A()
// do construction work
constructed = true;
foo()
if (not constructed)
throw std::logic_error("Cannot call foo() during construction");
// the rest of foo
protected:
bool constructed false ;
You could also make these checks only apply when compiling in DEBUG mode (e.g. with conditional compilation using the preprocessor - #ifndef NDEBUG
) so that at run time you won't get the performance penalty. Mind the noexcept
s though.
An alternative to throwing could be assert()
'ing.
Since apparently there isn't compile-time solution which doesn't make code less readable, I decided to go withassert(!wptr.expired())
. I think it's a little bit more fitting than exception, because there is no way to recover from this situation.
– Korri
Apr 8 at 17:23
@Korri remember thatassert
s are usually compiled out in release builds, so nothing will happen. An exception however is not, so it would still terminate the program (if not caught and swallowed) in a release build. You could do both; firstassert
thenthrow
, or justthrow
.
– Jesper Juhl
Apr 8 at 18:38
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55576192%2fis-there-a-way-to-make-member-function-not-callable-from-constructor%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I am afraid the answer is "no, it's not possible to protect against this at compile-time." It's always difficult to prove a negative, but consider this: if it was possible to protect a function this way, it would probably have been done for weak_from_this
and shared_from_this
in the standard library itself.
add a comment |
I am afraid the answer is "no, it's not possible to protect against this at compile-time." It's always difficult to prove a negative, but consider this: if it was possible to protect a function this way, it would probably have been done for weak_from_this
and shared_from_this
in the standard library itself.
add a comment |
I am afraid the answer is "no, it's not possible to protect against this at compile-time." It's always difficult to prove a negative, but consider this: if it was possible to protect a function this way, it would probably have been done for weak_from_this
and shared_from_this
in the standard library itself.
I am afraid the answer is "no, it's not possible to protect against this at compile-time." It's always difficult to prove a negative, but consider this: if it was possible to protect a function this way, it would probably have been done for weak_from_this
and shared_from_this
in the standard library itself.
answered Apr 8 at 14:59
AngewAngew
135k11261354
135k11261354
add a comment |
add a comment |
No there is no way. Consider:
void call_me(struct widget*);
struct widget : std::enable_shared_from_this<widget>
widget()
call_me(this);
void display()
shared_from_this();
;
// later:
void call_me(widget* w)
w->display(); // crash
The thing is there is a reason you want to check for not calling shared_from_this
in the constructor. Think about that reason. It's not that shared_from_this
cannot be called, it's because it's return value has no way of being assigned yet. It is also not because it will never be assigned. It's because it will be assigned later in the execution of the code. Order of operation is a runtime property of your program. You cannot assert at compile time for order of operation, which is done at runtime.
add a comment |
No there is no way. Consider:
void call_me(struct widget*);
struct widget : std::enable_shared_from_this<widget>
widget()
call_me(this);
void display()
shared_from_this();
;
// later:
void call_me(widget* w)
w->display(); // crash
The thing is there is a reason you want to check for not calling shared_from_this
in the constructor. Think about that reason. It's not that shared_from_this
cannot be called, it's because it's return value has no way of being assigned yet. It is also not because it will never be assigned. It's because it will be assigned later in the execution of the code. Order of operation is a runtime property of your program. You cannot assert at compile time for order of operation, which is done at runtime.
add a comment |
No there is no way. Consider:
void call_me(struct widget*);
struct widget : std::enable_shared_from_this<widget>
widget()
call_me(this);
void display()
shared_from_this();
;
// later:
void call_me(widget* w)
w->display(); // crash
The thing is there is a reason you want to check for not calling shared_from_this
in the constructor. Think about that reason. It's not that shared_from_this
cannot be called, it's because it's return value has no way of being assigned yet. It is also not because it will never be assigned. It's because it will be assigned later in the execution of the code. Order of operation is a runtime property of your program. You cannot assert at compile time for order of operation, which is done at runtime.
No there is no way. Consider:
void call_me(struct widget*);
struct widget : std::enable_shared_from_this<widget>
widget()
call_me(this);
void display()
shared_from_this();
;
// later:
void call_me(widget* w)
w->display(); // crash
The thing is there is a reason you want to check for not calling shared_from_this
in the constructor. Think about that reason. It's not that shared_from_this
cannot be called, it's because it's return value has no way of being assigned yet. It is also not because it will never be assigned. It's because it will be assigned later in the execution of the code. Order of operation is a runtime property of your program. You cannot assert at compile time for order of operation, which is done at runtime.
answered Apr 8 at 15:37
Guillaume RacicotGuillaume Racicot
16.5k53872
16.5k53872
add a comment |
add a comment |
Not as such, but - if performance is not an issue, you could add a flag which indicates construction is complete, and use that to fail at run-time with such calls:
class A
// ... whatever ...
public:
A()
// do construction work
constructed = true;
foo()
if (not constructed)
throw std::logic_error("Cannot call foo() during construction");
// the rest of foo
protected:
bool constructed false ;
You could also make these checks only apply when compiling in DEBUG mode (e.g. with conditional compilation using the preprocessor - #ifndef NDEBUG
) so that at run time you won't get the performance penalty. Mind the noexcept
s though.
An alternative to throwing could be assert()
'ing.
Since apparently there isn't compile-time solution which doesn't make code less readable, I decided to go withassert(!wptr.expired())
. I think it's a little bit more fitting than exception, because there is no way to recover from this situation.
– Korri
Apr 8 at 17:23
@Korri remember thatassert
s are usually compiled out in release builds, so nothing will happen. An exception however is not, so it would still terminate the program (if not caught and swallowed) in a release build. You could do both; firstassert
thenthrow
, or justthrow
.
– Jesper Juhl
Apr 8 at 18:38
add a comment |
Not as such, but - if performance is not an issue, you could add a flag which indicates construction is complete, and use that to fail at run-time with such calls:
class A
// ... whatever ...
public:
A()
// do construction work
constructed = true;
foo()
if (not constructed)
throw std::logic_error("Cannot call foo() during construction");
// the rest of foo
protected:
bool constructed false ;
You could also make these checks only apply when compiling in DEBUG mode (e.g. with conditional compilation using the preprocessor - #ifndef NDEBUG
) so that at run time you won't get the performance penalty. Mind the noexcept
s though.
An alternative to throwing could be assert()
'ing.
Since apparently there isn't compile-time solution which doesn't make code less readable, I decided to go withassert(!wptr.expired())
. I think it's a little bit more fitting than exception, because there is no way to recover from this situation.
– Korri
Apr 8 at 17:23
@Korri remember thatassert
s are usually compiled out in release builds, so nothing will happen. An exception however is not, so it would still terminate the program (if not caught and swallowed) in a release build. You could do both; firstassert
thenthrow
, or justthrow
.
– Jesper Juhl
Apr 8 at 18:38
add a comment |
Not as such, but - if performance is not an issue, you could add a flag which indicates construction is complete, and use that to fail at run-time with such calls:
class A
// ... whatever ...
public:
A()
// do construction work
constructed = true;
foo()
if (not constructed)
throw std::logic_error("Cannot call foo() during construction");
// the rest of foo
protected:
bool constructed false ;
You could also make these checks only apply when compiling in DEBUG mode (e.g. with conditional compilation using the preprocessor - #ifndef NDEBUG
) so that at run time you won't get the performance penalty. Mind the noexcept
s though.
An alternative to throwing could be assert()
'ing.
Not as such, but - if performance is not an issue, you could add a flag which indicates construction is complete, and use that to fail at run-time with such calls:
class A
// ... whatever ...
public:
A()
// do construction work
constructed = true;
foo()
if (not constructed)
throw std::logic_error("Cannot call foo() during construction");
// the rest of foo
protected:
bool constructed false ;
You could also make these checks only apply when compiling in DEBUG mode (e.g. with conditional compilation using the preprocessor - #ifndef NDEBUG
) so that at run time you won't get the performance penalty. Mind the noexcept
s though.
An alternative to throwing could be assert()
'ing.
edited Apr 9 at 7:29
answered Apr 8 at 16:58
einpoklumeinpoklum
37.4k28135264
37.4k28135264
Since apparently there isn't compile-time solution which doesn't make code less readable, I decided to go withassert(!wptr.expired())
. I think it's a little bit more fitting than exception, because there is no way to recover from this situation.
– Korri
Apr 8 at 17:23
@Korri remember thatassert
s are usually compiled out in release builds, so nothing will happen. An exception however is not, so it would still terminate the program (if not caught and swallowed) in a release build. You could do both; firstassert
thenthrow
, or justthrow
.
– Jesper Juhl
Apr 8 at 18:38
add a comment |
Since apparently there isn't compile-time solution which doesn't make code less readable, I decided to go withassert(!wptr.expired())
. I think it's a little bit more fitting than exception, because there is no way to recover from this situation.
– Korri
Apr 8 at 17:23
@Korri remember thatassert
s are usually compiled out in release builds, so nothing will happen. An exception however is not, so it would still terminate the program (if not caught and swallowed) in a release build. You could do both; firstassert
thenthrow
, or justthrow
.
– Jesper Juhl
Apr 8 at 18:38
Since apparently there isn't compile-time solution which doesn't make code less readable, I decided to go with
assert(!wptr.expired())
. I think it's a little bit more fitting than exception, because there is no way to recover from this situation.– Korri
Apr 8 at 17:23
Since apparently there isn't compile-time solution which doesn't make code less readable, I decided to go with
assert(!wptr.expired())
. I think it's a little bit more fitting than exception, because there is no way to recover from this situation.– Korri
Apr 8 at 17:23
@Korri remember that
assert
s are usually compiled out in release builds, so nothing will happen. An exception however is not, so it would still terminate the program (if not caught and swallowed) in a release build. You could do both; first assert
then throw
, or just throw
.– Jesper Juhl
Apr 8 at 18:38
@Korri remember that
assert
s are usually compiled out in release builds, so nothing will happen. An exception however is not, so it would still terminate the program (if not caught and swallowed) in a release build. You could do both; first assert
then throw
, or just throw
.– Jesper Juhl
Apr 8 at 18:38
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55576192%2fis-there-a-way-to-make-member-function-not-callable-from-constructor%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Note there is a difference in scope between a child class constructor body and the parent class constructor: the latter has been executed completely before you even start initializing the child class's members (if any), let alone enter the child class constructor body.
– rubenvb
Apr 8 at 14:58
4
Nice question. One way would be to make a dummy class with pure virtual function
weak_from_this
and inherit yours from it. This will make it a hard compile error.– SergeyA
Apr 8 at 14:59
5
@SergeyA Why didn't you post that as an answer? All other people here seem to conclude that it's not possible so either your comment is wrong and misleading or they are wrong and you should show how it can be achieved.
– Bakuriu
Apr 8 at 21:17
@Bakuriu well, I did not have the energy to polish it to the full blown answer. It is possible that it is not a workable solution.
– SergeyA
Apr 9 at 14:07
Simply based on the fact the static type of an object doesn't (and cannot) depend on the fact its construction is completed. Note that if that were the case, you wouldn't be able in a ctor of
T
to storethis
in a global obj register, and use the obj later, w/o a cast, as the ptr stored would have "(in construction)T*" not plainT*
.– curiousguy
13 hours ago