Why couldn't deduce const& template parameter?

允我心安 提交于 2020-01-03 17:05:34

问题


Consider this code:

constexpr int TEN = 10;

template < const int& >
struct Object { };

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

Then the calls:

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

The second call fails to compile with error message:

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’

The question is why? Is it according to the standard?

And the more important question is: how can I workaround this? That is: how can I help the compiler to deduce the const int& template parameter?

In the real code instead of int I have more complex literal type, so I do need the const&. Thus I can't just "use int instead of const int&".

I am using gcc-7.0.1 (the snapshot) and I am getting the same error with options -std=c++11, -std=c++14, -std=c++17.

来源:https://stackoverflow.com/questions/42111351/why-couldnt-deduce-const-template-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!