Vah*_*agn 5 c++ parameters templates non-type c++11
考虑这段代码:
\n\nconstexpr int TEN = 10;\n\ntemplate < const int& >\nstruct Object { };\n\ntemplate < const int& II >\nvoid test(Object<II>) { }\nRun Code Online (Sandbox Code Playgroud)\n\n然后调用:
\n\ntest<TEN>(Object<TEN>{}); // passes\ntest(Object<TEN>{}); // FAILS\nRun Code Online (Sandbox Code Playgroud)\n\n第二次调用无法编译并显示错误消息:
\n\nerror: no matching function for call to \xe2\x80\x98test(Object<TEN>)\nnote: candidate: template<const int& II> void test(Object<II>)\nnote: template argument deduction/substitution failed:\nnote: couldn\'t deduce template parameter \xe2\x80\x98II\xe2\x80\x99\nRun Code Online (Sandbox Code Playgroud)\n\n问题是为什么?是否符合标准?
\n\n更重要的问题是:我该如何解决这个问题?即:如何帮助编译器推导const int&模板参数?
而在真正的代码中int我却更复杂literal type,所以我确实需要const&. 因此我不能只是“使用int而不是const int&”。
我正在使用(快照),并且选项, ,gcc-7.0.1遇到相同的错误。-std=c++11-std=c++14-std=c++17