mercredi 29 mars 2023

variadic class template can't compile

template<typename... Types>
class Tuple;

// recursive case:
template<typename Head, typename... Tail>
class Tuple<Head, Tail...>
{
 private:
  Head head;
  Tuple<Tail...> tail;
 public:
  // constructors:
  Tuple() {
  }
  Tuple(Head const& h, Tuple<Tail...> const& t)
    : head(h), tail(t) {
  }
  //...

  Head& getHead() { return head; }
  Head const& getHead() const { return head; }
  Tuple<Tail...>& getTail() { return tail; }
  Tuple<Tail...> const& getTail() const { return tail; }
};

// basis case:
template<>
class Tuple<> {
  // no storage required
};

int main()
{
    Tuple<int,double> (1,4.5);
}

compiling it with g++ 11.3.0, I get this errors:

error: no matching function for call to ‘Tuple<int, double>::Tuple(int, double)’

why does this error happen and how to fix them?

Aucun commentaire:

Enregistrer un commentaire