我试图将其减少到最低限度:
#include <array>
template <std::size_t N>
void f(int, std::array<int, N> const & =
std::array<int, 0>()) {
}
int main() {
f(10);
}
array_test.cpp:4:6: 注意:模板参数推导/替换失败: array_test.cpp:10:9: 注意:无法推断模板参数“N” f(10);
为什么会失败?我不明白:它应该可以从默认参数中推导出来。我需要一个解决方法。
最佳答案
你需要给N
一个默认值,而不是数组:
template <std::size_t N = 0>
void f(int, std::array<int, N> const & =
std::array<int, N>()) {
}
至于为什么不能从默认值推导出N
,参见Why can't the compiler deduce the template type from default arguments? .
关于c++ - 无法推断模板参数 'N',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41587485/