在C++标准[temp.over.link]中,解释了函数模板等价性的确定不应涉及编译器的“英勇努力”。
例如,C++ 标准提出了以下建议:
// 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>);
Run Code Online (Sandbox Code Playgroud)
此规则是否也适用于涉及元编程的情况,如下例所示?
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);
Run Code Online (Sandbox Code Playgroud)
让我们从简单的案例开始:
template <typename T> using id = T;
template<class T> T f(T);
template<class T> id<T> f(T);
Run Code Online (Sandbox Code Playgroud)
对我来说,这显然是不正确的,根据相关规则不需要诊断。这两个声明在功能上是等效的,但不是简单地重命名模板参数(并且没有任何依赖名称需要考虑)。
对于更复杂的情况:
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);
Run Code Online (Sandbox Code Playgroud)
我认为这可能不是格式错误,因为它们在功能上并不是真正等效的。可能存在id_t这样的特殊化,这两个声明实际上是不同的 - 所以这些实际上是不同的声明。
| 归档时间: |
|
| 查看次数: |
194 次 |
| 最近记录: |