为什么无法推导出 const& 模板参数?

Vah*_*agn 5 c++ parameters templates non-type c++11

考虑这段代码:

\n\n
constexpr int TEN = 10;\n\ntemplate < const int& >\nstruct Object { };\n\ntemplate < const int& II >\nvoid test(Object<II>) { }\n
Run Code Online (Sandbox Code Playgroud)\n\n

然后调用:

\n\n
test<TEN>(Object<TEN>{}); // passes\ntest(Object<TEN>{}); // FAILS\n
Run Code Online (Sandbox Code Playgroud)\n\n

第二次调用无法编译并显示错误消息:

\n\n
error: 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\n
Run Code Online (Sandbox Code Playgroud)\n\n

问题是为什么?是否符合标准?

\n\n

更重要的问题是:我该如何解决这个问题?即:如何帮助编译器推导const int&模板参数?

\n\n

而在真正的代码中int我却更复杂literal type,所以我确实需要const&. 因此我不能只是“使用int而不是const int&”。

\n\n

我正在使用(快照),并且选项, ,gcc-7.0.1遇到相同的错误。-std=c++11-std=c++14-std=c++17

\n