mercredi 31 janvier 2018

decltype() variadic template base class

I have the following code where I'm expecting decltype() to not work on Derived class to get run() base class method return-type, since the base class does not have a default constructor.

class Base
{
  public:
    int run() { return 1; }

  protected:
    Base(int){}
}; 

struct Derived : Base
{
  template <typename ...Args>
  Derived(Args&... args) : Base{args...}
  {}
};

int main()
{
  decltype(Derived{}.run()) v {10}; // it works. Not expected since
                                    // Derived should not have default constructor
  std::cout << "value: " << v << std::endl;

  //decltype(Base{}.run()) v1 {10}; // does not work. Expected since 
                                    // Base does not have default constructor
  //std::cout << "value: " << v1 << std::endl;
}

I'm aware you can use declval<> to get member functions without going through constructors, but my question is why decltype works here. I tried to find in the C++ standard something relevant, but did not find anything. Also tried multiple compilers including clang and have the same behavior.

Aucun commentaire:

Enregistrer un commentaire