简体   繁体   English

候选模板被忽略:替换失败(clang但不是g ++错误)

[英]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.

相关问题 “忽略候选模板:替换失败:”编译器错误? - "candidate template ignored: substitution failure:" compiler error? clang: 候选模板被忽略: 替换失败: typedef &#39;type&#39; 不能用类说明符引用 - clang: candidate template ignored: substitution failure: typedef 'type' cannot be referenced with a class specifier 忽略Clang候选模板:替换失败(也无法使用gcc编译,VC工作正常) - Clang candidate template ignored: substitution failure (also fails to compile with gcc, VC works fine) “候选模板被忽略:替换失败”错误帮助,C ++,犰狳,rank(), - 'candidate template ignored: substitution failure' error help, C++, Armadillo, rank(), 使用lambdas的变量模板:使用g ++但使用clang ++运行时出错 - Variadic template using lambdas : error with g++ but running with clang++ constexpr静态模板函数:g ++错误是对clang的警告 - constexpr static template function: g++ error is a warning on clang clang 3.3中的模板默认Arg替换失败 - Template Default Arg Substitution failure in clang 3.3 模板元编程 - g ++吃它,clang没有 - template metaprogramming - g++ eats it, clang does not 用于模板实例化的Clang / g ++选项 - Clang/g++ options for template instantiation 文件系统库的编译器错误:clang 和 g++ - Compiler error with filesystem library: clang and g++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM