mercredi 2 septembre 2015

Non-terminating recursive use of decltype

Compilation of the following code has problems due to infinite compile-time recursion. Clang 3.6.0 gives an error about recursive template instantiation depth, and doesn't terminate; while GCC 4.9.2 remains silent, and also doesn't terminate.

This is a simpler example than I was originally faced with, and certainly the second overload of bar could be given int as a return type; in this case, the first overload is chosen for the call to bar in main; as I'd originally hoped. Why does the application of decltype not resolve to the first overload, which is specialised for Foo?

The second (default) template parameter of Foo seems to hide more verbose messages regarding the recursion issue here. This would have been useful in the original context.

template <typename T, typename A = T>
struct Foo {};

template <typename T, typename A>
int bar(const Foo<T,A> &x) { return 0; }

template <typename T>
auto bar(const T &x)
 -> decltype(bar(Foo<T>{})){    
    return   bar(Foo<T>{});    
}

int main(int argc, char *argv[])
{
  Foo<char> f;    
  bar(f);

  return 0;
}

Aucun commentaire:

Enregistrer un commentaire