模板等效还是模板功能等效? [英] Template equivalence or template functional equivalence?
问题描述
在C++标准[temp.over.link]中,解释了函数模板等价性的确定不应涉及编译器的英勇努力".
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.
举个例子,C++ 标准提出了这个:
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);
推荐答案
让我们从简单的案例开始:
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).
更复杂的情况:
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);
我认为这可能不是格式错误,因为它们在功能上并不真正等效.可能存在 id_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.
这篇关于模板等效还是模板功能等效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!