For some complicated reasons I want to create default constructor (alongside my normal constructors) that always throws. I want it to be there, but I also want it never to be called. It is pretty obvious that during runtime I can check for that thrown exception and for example terminate program when I catch it, but the ideal solution would be to have it checked during compilation.
So my question is: can I statically assert somehow that a function will never be called? I've looked at functions in <type_traits>
but I don't see anything there that would help me. Is there some c++ dark magic that I could use to achieve my goal?
I don't have a code example, because what would even be in there?
PS. Yes. I am sure that I want to have a function and disallow everybody of calling it. As I stated previously reasons for that are complicated and irrelevant to my question.
EDIT. I can't delete
this constructor or make it private
. It has to be accessible for deriving classes, but they shouldn't call it. I have a case of virtual inheritance and want to "allow" calling this constructor by directly virtually derived classes (they won't call it, but c++ still requires it to be there), but no in any other classes deeper in inheritance chain.
EDIT 2. As requested I give a simplified example of my code.
#include <stdexcept>
class Base {
protected:
Base() { throw std::logic_error{"Can't be called"}; }
Base(int); // proper constructor
private:
// some members initialized by Base(int)
};
class Left: virtual public Base {
protected:
Left(int) {}
// ^ initialize members of Left, but does not call Base()!
// Though it seems that it implicitly does, Base() is never actually called.
};
class Right: virtual public Base {
protected:
Right(int) {} // The same as in Left
};
class Bottom: public Left, public Right {
public:
Bottom(int b, int l, int r): Base{b}, Left{l}, Right{r} {}
// ^ Explicitly calling constructors of Base, Left, Right.
// If I forget about calling Base(int) it silently passes
// and throws during runtime. Can I prevent this?
};
EDIT 3. Added body to Left's and Right's constructors, so that they implicitly "call" Base()
.