0

C++ compilers warn if pure virtual functions are directly called in a constructor/destructor (but not indirectly).

Binding a pure virtual function and storing the result in a class member and calling that member all from the same constructor is just a pure virtual call with extra steps.

In this context, std::bind has the advantage that the compiler (or STL) has all the information it needs to do the check: it has the member function pointer and it knows it's in a constructor.

If one could convert std::bind into a macro that inserts a call to the pure virtual function before returning the bind result, it would allow for statically checking against binding pure virtual functions in a codebase. But apparently one can't just call &SomeClass::func(this, placeholders::_1, placeholders::_2, etc) in a macro.

Is there a way to get the compiler to warn that a member function pointer is pure virtual without it being actually called from the constructor ?

asked Nov 4, 2024 at 11:50
13
  • 1
    You could eliminate this problem by construction if you replaced such uses of std::bind by equivalent lambda functions.. Commented Nov 4, 2024 at 11:56
  • "But apparently one can't just call ... in a macro." Yep. Do you know how to call a member function pointer? Commented Nov 4, 2024 at 11:57
  • Binding the function or storing a member pointer to it are not the actions that are problematic. Only an actual call from the constructor causes problems. Determining whether an indirect call happens while the variable stores such a problematic value is much harder and generally an undecidable problem. Commented Nov 4, 2024 at 11:57
  • &SomeClass::func(this, placeholders::_1, placeholders::_2, etc) is just wrong syntax for a member function call. But regardless, how would that help you? Do you want all uses of bind in your program to implicitly cause a call to the function that is being bound? Surely that's not correct behavior... Commented Nov 4, 2024 at 11:59
  • 1
    @Peter-ReinstateMonica: doing binding for immediate call seems strange anyway, why not call the function directly in that case? Commented Nov 4, 2024 at 12:56

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.