[英]candidate template ignored: substitution failure(error with clang but not g++)
I have a problem of substitution failure, and answers of some similar questions do not help me. 我有一个替换失败的问题,一些类似问题的答案对我没有帮助。
Here is the code: 这是代码:
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)
{
// ...
}
// ...
};
And I call this template function like so: 我称这个模板函数如下:
int main()
{
// ...
const int DENOISE_UR[3][4] = {/*...*/};
Partition<1,2> partition;
partition.readPattern(DENOISE_UR);
// ...
}
Using g++, it compiles. 使用g ++编译。
When using clang++(linux) to compile( clang++ -std=c++11 xxx.cpp
), it resulted in the following compiling error: 当使用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)
Why? 为什么?
It's a bug in clang; 这是一个铿锵的错误; it misbehaves when an alias template defining an array type is defined within a class template.
当在类模板中定义定义数组类型的别名模板时,它会出错。 In fact it can be exploited to crash the compiler :
事实上,它可以被利用来崩溃编译器 :
template<int I>
struct S {
template<int J> using T = int[J];
using U = T<I>;
};
S<3>::U a;
Since in your case Reference::matrix_t
does not depend on the template arguments to Reference
, the simplest workaround would be to move the definition of matrix_t
to namespace scope: 因为在你的情况下,
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>;
In fact, you don't even need to use impl::matrix_t
to workaround the bug: 实际上,您甚至不需要使用
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!
This is now fixed (the fix should be in clang release version 3.8.0): 现在已经修复了 (修复程序应该在clang发行版本3.8.0中):
[AST] Perform additional canonicalization for DependentSizedArrayType
[AST]为DependentSizedArrayType执行其他规范化
We treated DependentSizedArrayTypes with the same element type but differing size expressions as equivalently canonical.
我们使用相同的元素类型处理DependentSizedArrayTypes,但不同的大小表达式等同于规范。 This would lead to bizarre behavior during template instantiation.
这会在模板实例化期间导致奇怪的行为。
This fixes PR24212.
这修复了PR24212。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.