Template argument deduction inconsistent

Viewed 101

The following code compiles and runs on MSVC 2019 and Clang trunk. (I think it needs at least C++17). It does not run on gcc-trunk and I believe the consensus is that this is due to a bug in gcc.

However, when any of the elements are replaced by a user type or a pointer type, it fails on all compilers. To see this, uncomment the tuple_c definition near the end.

I'm actually slightly surprised this works at all since it appears to be specializing a function with a universal-ref parameter with one that has an r-value-ref parameter. Maybe that's ok? If it is, why is it failing with the struct?

Is there a better way of writing this? I mean in general. I'm well aware of std::tuple.

#include <iostream>
using namespace std;
template <typename... TP> class Tuple
{
};
template <> class Tuple <>
{
};
template <typename Head, typename... Tail> class Tuple <Head, Tail...>
{
  Head head;

  Tuple <Tail...> tail;
public:
  Tuple ()
  {
  }

  Tuple (const Head& head_in, const Tail&...tail_in)
    : head (head_in), tail (tail_in...)
  {
  }
  template <int i> auto Get ()
  {
    return tail.template Get <i-1> ();
  }

  template <> auto Get <0> ()
  {
    return head;
  }

  template <int i, typename T> void Set (T&& v) // T&& is a universal ref
  {
    tail.template Set <i-1, T> (static_cast <T&&> (v));
  }
  template <int i, typename T> void Set (const T& v)
  {
    tail.template Set <i-1, T> (v);
  }
  template <> void Set <0, Head> (Head&& v) // Head&& is an rv-ref
  {
    head = v;
  }
  template <> void Set <0, Head> (const Head& v)
  {
    head = v;
  }
};
template <typename Head, typename... Tail> Tuple <Head, Tail...> MakeTuple (Head&& head, Tail&&...tail)
{
  Tuple <Head, Tail...> result (head, tail...);

  return result;
}
struct S
{
  int x;
  int y;
};
ostream& operator << (ostream& out, const S& s)
{
  out << "{" << s.x << "," << s.y << "}";
  return out;
}
int main(int argc, char* argv[])
{

  auto tuple_a = MakeTuple (1,2,3,4);
  tuple_a.Set <1,int> (42);
  cout << tuple_a.Get <0> () << '\n';
  cout << tuple_a.Get <1> () << '\n';
  cout << tuple_a.Get <2> () << '\n';
  cout << tuple_a.Get <3> () << '\n';
  auto tuple_b = MakeTuple (1,2.3f,3,4);
  tuple_b.Set <1,float> (42.3f);
  cout << tuple_b.Get <0> () << '\n';
  cout << tuple_b.Get <1> () << '\n';
  cout << tuple_b.Get <2> () << '\n';
  cout << tuple_b.Get <3> () << '\n';

  S s {4,5};
  //auto tuple_c = MakeTuple (1,2.3f,3,s);
  return 0;
}
2 Answers

First, before CWG 727 you're not allowed to specialize a member function template inside a class scope. You will have to use constexpr-if, tag-dispatching or SFINAE to handle the i==0 case.

In using std::enable_if_t that would be:

template <int i, typename T>
std::enable_if_t<i != 0> Set(T&& v)
{
    tail.template Set<i-1>(static_cast<T&&>(v));
}

template <int i, typename T>
std::enable_if_t<i == 0> Set(T&& v)
{
    head = static_cast<T&&>(v);
} 

In using constexpr-if it becomes:

template <int i, typename T>
void Set(T&& v)
{
    if constexpr (i == 0) head = static_cast<T&&>(v);
    else tail.template Set<i-1>(static_cast<T&&>(v));
}

Secondly, once compilers allow you to specialize a function template inside a class cope, there is another problem with your current approach. Your MakeTuple implementation, due to how template argument deduction works for forwarding references, creates a tuple of reference types corresponding to those MakeTuple arguments that are lvalues:

template <typename Head, typename... Tail>
Tuple<Head, Tail...> MakeTuple(Head&& head, Tail&&... tail);

This makes your comment/assumption:

void Set<0, Head>(Head&& v) // Head&& is an rv-ref

invalid.

That is, for an lvalue expression s:

S s{ 4, 5 };
MakeTuple(s);

The deduced Head is S& (it's also the type of head after reference collapsing). Then the compiler attempts to instantiate Tuple<S&> and it generates the following two declarations:

void Set<0, S&>(S& && v); 

void Set<0, S&>(S& const& v);

After reference collapsing it ends up with:

void Set<0, S&>(S& v);

void Set<0, S&>(S& v);

At this point, not only both definitions are the same, but also the compiler can't decide which of the primary function templates:

template <int i, typename T>
void Set(T&& v);

template <int i, typename T>
void Set(const T& v);

they are specializations of, as using T=S& matches both. This could be solved by decaying each type before storing it in a tuple:

template <typename Head, typename... Tail>
Tuple<std::decay_t<Head>, std::decay_t<Tail>...> MakeTuple(Head&& head, Tail&&... tail);

My question was answered above but I thought it might be useful to include the complete code for the solution. It now works for all types except arrays and string literals.

#include <iostream>
using namespace std;

template <typename... TP> class Tuple
{
};

template <> class Tuple <>
{
};

template <typename Head, typename... Tail> class Tuple <Head, Tail...>
{
  Head head;
  Tuple <Tail...> tail;

public:
  Tuple ()
  {
  }

  Tuple (const Head& head_in, const Tail&...tail_in)
    : head (head_in), tail (tail_in...)
  {
  }

  template <int i> auto Get ()
  {
    return tail.template Get <i-1> ();
  }

  template <> auto Get <0> ()
  {
    return head;
  }

  template <int i, typename T> void Set (T&& v) 
  {
    tail.template Set <i-1, T> (static_cast <T&&> (v));
  }

  template <int i, typename T> void Set (const T& v)
  {
    tail.template Set <i-1, typename std::decay<T>::type> (v);
  }

  template <> void Set <0, typename std::decay<Head>::type> (Head&& v) 
  {
    head = v;
  }

  template <> void Set <0, typename std::decay<Head>::type> (const Head& v)
  {
    head = v;
  }
};

template <typename Head, typename...Tail> Tuple <typename std::decay <Head>::type, typename std::decay<Tail>::type...> MakeTuple (Head&& head, Tail&&...tail)
{
  Tuple <typename std::decay <Head>::type, typename std::decay<Tail>::type...> result (head, tail...);
  return result;  
}

struct S
{
  int x;
  int y;
};

ostream& operator << (ostream& out, const S& s)
{
  out << "{" << s.x << "," << s.y << "}";
  return out;
}

int main(int argc, char* argv[])
{
  const char* p = "hello";
  S s;
  int v = 32;

  auto tuple_a = MakeTuple (1.0,v,p,s);

  cout << tuple_a.Get <0> () << endl;
  cout << tuple_a.Get <1> () << endl;
  cout << tuple_a.Get <2> () << endl;
  cout << tuple_a.Get <3> () << endl;

  S s_update {10,12};

  tuple_a.Set <3> (s_update);

  const char* p_update = "goodbye";

  tuple_a.Set <2> (p_update);

  cout << tuple_a.Get <0> () << endl;
  cout << tuple_a.Get <1> () << endl;
  cout << tuple_a.Get <2> () << endl;
  cout << tuple_a.Get <3> () << endl;
}
Related