Can I make the constructor of a derived class constexpr if the base class constructor is not constexpr?

Viewed 592

I am trying to compile an example from "Design Patterns" and I am facing the following problem:

I have a base class MapSite:

class MapSite{
public:
    MapSite();
    virtual ~MapSite(); 
    virtual void Enter() = 0;
};

and a derived class Room:

class Room : public MapSite final{

private:
    unsigned int room_number;

public:
    Room(unsigned int rn) : room_number(rn) {};

    virtual void Enter() override;
};

From another class I want to call the function

virtual std::unique_ptr<Room> MakeRoom(unsigned int n) {return make_unique<Room(n)>();} 

When I do so I get the following error:

error: temporary of non-literal type ‘Room’ in a constant expression
  virtual std::unique_ptr<Room> MakeRoom(unsigned int n) {return unique::make_unique<Room(n)>();}

So I thought the problem may be that the constructor has to be constexpr in order to call the constructor of Room from another function, but setting the constructor to:

constexpr Room(unsigned int rn) : room_number(rn) {};

will produce this error:

error: call to non-constexpr function ‘MapSite::MapSite()’
  constexpr Room(unsigned int rn) : room_number(rn) {};

My basic question is whether I can make a derived class constructor constexpr, even if the base class constructor is not. Or maybe if there is a much better concept to solve this problem.

PS: make_unique is a c++14 feature which I emulated from here How to implement make_unique function in C++11? for c++11, which I am compiling with

2 Answers
Related