C++ 模板类型确定问题

flying_music flying_music 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
大神帮忙看一下下面的代码为什么不能编译通过

#include <iostream>
#include <string>

using namespace std;

template <typename Type>
void printElements(const Type& col)
{
for(const auto &item : col){
cout << item << endl;
}
}

int main()
{
auto a = {1, 2, 3, 4};
printElements(a); //OK
printElements({1, 2, 3, 4}); //ERROR: couldn't deduce template parameter ‘Type’
return 0;
}

既然auto可以推导出{1,2,3,4}的类型,然后调用printElements函数,那下面直接用{1,2,3,4}去调用就会提示不能确定类型呢?
...全文

相关推荐

评论 7
收藏 已收藏
分享
返回顶部
flying_music
C++ 语言 65,214 成员 250,514 内容