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