Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template equivalence or template functional equivalence?

Tags:

c++

templates

In the C++ standard [temp.over.link], it is explained that the determination of function template equivalence should not involve "heroic efforts" of the compiler.

As an example, the C++ standard propose this:

// guaranteed to be the same
template <int I> void f(A<I>, A<I+10>);
template <int I> void f(A<I>, A<I+10>);
// guaranteed to be different
template <int I> void f(A<I>, A<I+10>);
template <int I> void f(A<I>, A<I+11>);
// ill-formed, no diagnostic required
template <int I> void f(A<I>, A<I+10>);
template <int I> void f(A<I>, A<I+1+2+3+4>);

Is this rule also applies to cases involving meta programming, as in the example below?

template<class T>
struct t_{
   using type = T;
   };
//ill-formed or different?
template<class T> T f(T);
template<class T> typename t_<T>::type f(T);
like image 876
Oliv Avatar asked Dec 06 '17 16:12

Oliv


1 Answers

Let's start with the simple case:

template <typename T> using id = T;

template<class T> T f(T);
template<class T> id<T> f(T);

This to me is clearly ill-formed, no diagnostic required, per the relevant rule. The two declarations are functionally equivalent, but not in a way that's simply renaming template parameters (and there aren't any dependent names to consider).


With the more complicated case:

template <typename T> struct id_t { using type = T; };
template <typename T> using id = typename id_t<T>::type;

template<class T> T f(T);
template<class T> id<T> f(T);

I think this is probably not ill-formed, because they aren't truly functionally equivalent. There could be specializations of id_t such that these two declarations are actually different - so these are actually different declarations.

like image 85
Barry Avatar answered Sep 20 '22 21:09

Barry