![](/img/trans.png)
[英]c++ template recursion double linked list error gcc (but clang++ accepts)
[英]C++ template that used to work in old gcc results in 'shadows template parameter' error in clang++
我使用gcc 3.x编写了大约'08的代码。 我现在正在尝试使用clang 3.4进行编译,并且我收到了一个我不明白的模板错误。 我们的想法是声明任意维度和精度的固定维度vec类型,然后根据这些类型定义vecPair类型。 我不明白“a.convert()”中模板typename S的使用是如何遮蔽模板参数的; 它意味着使用参数,而不是重新声明它。 任何信息将不胜感激!
typedef unsigned int Uns;
template <typename T>
inline const T& min(const T& a, const T& b) {
return a <= b ? a : b;
}
template <Uns N, typename T>
struct vec {
T comp[N];
template <Uns M, typename S>
inline vec<M, S> convert() const {
vec<M, S> converted;
for (Uns i = 0; i < min(M, N); ++i) converted[i] = comp[i];
for (Uns i = N; i < M; ++i) converted[i] = 0;
return converted;
}
};
template <Uns N, typename T>
struct vecPair {
vec<N, T> a;
vec<N, T> b;
inline vecPair(const vec<N, T>& _a, const vec<N, T>& _b) : a(_a), b(_b) {}
template <Uns M, typename S>
inline vecPair<M, S> convert() const {
vec<M, S> ca = a.convert<M, S>();
vec<M, S> cb = b.convert<M, S>();
return vecPair<M, S>(ca, cb);
}
};
clang 3.4给出以下输出:
$ clang++ -fsyntax-only vec-bug.cpp
vec-bug.cpp:30:33: error: declaration of 'S' shadows template parameter
vec<M, S> ca = a.convert<M, S>();
^
vec-bug.cpp:28:29: note: template parameter is declared here
template <Uns M, typename S>
^
vec-bug.cpp:30:34: error: expected ';' at end of declaration
vec<M, S> ca = a.convert<M, S>();
^
;
vec-bug.cpp:31:12: error: template argument for template type parameter must be a type
vec<M, S> cb = b.convert<M, S>();
^
vec-bug.cpp:11:27: note: template parameter is declared here
template <Uns N, typename T>
^
...
这似乎有效:
vec<M, S> ca = a.template convert<M, S>();
vec<M, S> cb = b.template convert<M, S>();
我认为a
和b
具有依赖类型,因此您需要消除convert
是模板的歧义。 我不确定GCC为什么不介意。
更新:这似乎是一个已知的GCC错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.