C++ C++;具有非size\u t整数的std::数组的模板参数推导
我正试图根据我的需要调整中提出的解决方案。但是,我无法理解G++的行为。考虑以下功能:C++ C++;具有非size\u t整数的std::数组的模板参数推导,c++,arrays,c++11,integer,template-argument-deduction,C++,Arrays,C++11,Integer,Template Argument Deduction,我正试图根据我的需要调整中提出的解决方案。但是,我无法理解G++的行为。考虑以下功能: template <typename T, unsigned Size> int nextline(const typename std::array<T, Size> ar) { return 0; } 模板 int nextline(常量类型名称std::数组ar){ 返回0; } 然后电话 nextline(std::array<int, 2> {
template <typename T, unsigned Size>
int nextline(const typename std::array<T, Size> ar) {
return 0;
}
模板
int nextline(常量类型名称std::数组ar){
返回0;
}
然后电话
nextline(std::array<int, 2> { 1,0 });
nextline(std::array{1,0});
与GCC不匹配
eslong.cpp: In function ‘int main()’:
eslong.cpp:10:38: error: no matching function for call to ‘nextline(std::array<int, 2ul>)’
nextline(std::array<int, 2> { 1,0 });
^
eslong.cpp:10:38: note: candidate is:
eslong.cpp:4:5: note: template<class T, unsigned int Size> int nextline(std::array<T, Size>)
int nextline(const typename std::array<T, Size> ar) {
^
eslong.cpp:4:5: note: template argument deduction/substitution failed:
eslong.cpp:10:38: note: mismatched types ‘unsigned int’ and ‘#‘integer_cst’ not supported by dump_type#<type error>’
nextline(std::array<int, 2> { 1,0 });
^
eslong.cpp:10:38: note: ‘std::array<int, 2ul>’ is not derived from ‘std::array<T, Size>’
eslong.cpp:在函数“int main()”中:
eslong.cpp:10:38:错误:调用“nextline(std::array)”时没有匹配的函数
下一行(std::数组{1,0});
^
eslong.cpp:10:38:注:候选人为:
cpp:4:5:note:template int nextline(std::array)
int nextline(常量类型名称std::数组ar){
^
eslong.cpp:4:5:注意:模板参数扣除/替换失败:
eslong.cpp:10:38:注意:dump#u type#不支持不匹配的类型“unsigned int”和“#”integer_cst”
下一行(std::数组{1,0});
^
eslong.cpp:10:38:注意:“std::array”不是从“std::array”派生的
但是,如果我将
无符号大小
更改为无符号长大小
或大小
,它会匹配。我不确定这里发生了什么。调用std::array
中的大小
参数不是转换为大小
吗?您的文字2
会被解释为uns吗签名为long
,但您要将大小
模板声明为无符号int
。只需使用此模板即可:
template <typename T, size_t Size>
模板
模板化为:
template<class T, std::size_t N > struct array;
在这种情况下,即使您使用:
nextline(std::array<int, 2ul> { 1,0 });
nextline(std::array{1,0});
它仍然有效,因为它可以被演绎和铸造
dyp的补充说明: [temp.deletry.type]/17对于非类型模板参数,该参数要求导出对象(模板参数)的类型与为其导出的模板参数的类型相同 它对我来说编译得很好:1)这里的
typename
是错误的(const typename std::array…
)。2)您需要精确匹配非类型模板参数的类型,请参见[temp.decrete.type]/17。
nextline(std::array<int, 2ul> { 1,0 });