这段代码无法编译
template<unsigned n>
void test(char const (*)[n + 1]) { }
int main()
{
char const arr[] = "Hi";
test(&arr);
}
出现错误
note: candidate template ignored: couldn't infer template argument 'n'
然而,如果您将n+1
更改为n
,它会编译通过。
为什么编译器不能推断出在其上加了偏移量的n
?
void test(char const (*)[(n+3)*(n-2)])
,当传入一个由84个字符组成的数组时,你是否期望C++编译器将n
推断为9? - Sam Varshavchik