为什么不能推断出const& amp;模板参数?

时间:2017-02-08 11:03:19

标签: c++ c++11 templates parameters non-type

考虑以下代码:

constexpr int TEN = 10;

template < const int& >
struct Object { };

template < const int& II >
void test(Object<II>) { }

然后是电话:

test<TEN>(Object<TEN>{}); // passes
test(Object<TEN>{}); // FAILS

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

error: no matching function for call to ‘test(Object<TEN>)
note: candidate: template<const int& II> void test(Object<II>)
note:   template argument deduction/substitution failed:
note:   couldn't deduce template parameter ‘II’

问题是为什么?它符合标准吗?

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

在真实代码而不是int我有更复杂的literal type,所以我确实需要const&。因此,我无法使用int代替const int&&#34;。

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

0 个答案:

没有答案
相关问题