[英]candidate template ignored: substitution failure(error with clang but not g++)
我有一个替换失败的问题,一些类似问题的答案对我没有帮助。
这是代码:
template<int dim, int loop>
class Reference{
public:
//...
template<int r, int c> using matrix_t = int[r][c];
Reference(const matrix_t<dim, loop> &mat){}
};
template<int dim, int loop>
class Partition{
// ...
public:
// ...
template<int r, int c> using matrix = int[r][c];
template<int r, int c> void readPattern(const matrix<r,c> &pattern)
{
// ...
}
// ...
};
我称这个模板函数如下:
int main()
{
// ...
const int DENOISE_UR[3][4] = {/*...*/};
Partition<1,2> partition;
partition.readPattern(DENOISE_UR);
// ...
}
使用g ++编译。
当使用clang ++(linux)进行编译( clang++ -std=c++11 xxx.cpp
)时,会导致以下编译错误:
error: no matching function for call to 'readPattern'
note: candidate template ignored: substitution failure[ with r = 3, c = 4 ]
template<int r, int c> void readPattern(const matrix<r,c> &pattern)
为什么?
这是一个铿锵的错误; 当在类模板中定义定义数组类型的别名模板时,它会出错。 事实上,它可以被利用来崩溃编译器 :
template<int I>
struct S {
template<int J> using T = int[J];
using U = T<I>;
};
S<3>::U a;
因为在你的情况下, Reference::matrix_t
不依赖于Reference
的模板参数,最简单的解决方法是将matrix_t
的定义移动到命名空间作用域:
namespace impl { template<int r, int c> using matrix_t = int[r][c]; }
// ...
template<int dim, int loop>
class Reference {
//...
template<int r, int c> using matrix_t = impl::matrix_t<r, c>;
实际上,您甚至不需要使用 impl::matrix_t
来解决这个问题:
namespace magic { template<int r, int c> using unused = int[r][c]; } // Huh?
// ...
template<int dim, int loop>
class Reference {
//...
template<int r, int c> using matrix_t = int[r][c]; // Look ma, no hands!
现在已经修复了 (修复程序应该在clang发行版本3.8.0中):
[AST]为DependentSizedArrayType执行其他规范化
我们使用相同的元素类型处理DependentSizedArrayTypes,但不同的大小表达式等同于规范。 这会在模板实例化期间导致奇怪的行为。
这修复了PR24212。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.