Why could not the const & template parameter be output?

Consider this code:

constexpr int TEN = 10;

template < const int& >
struct Object { };

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

Then calls:

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

The second call does not compile with the 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? Does this meet the standard?

And an even more important question: how can I solve this? That is: how can I help the compiler to derive a template parameter const int&?

In real code, instead int, it's harder for me literal type, so I need it const&. So, I can’t just use intinstead const int&. "

I use the gcc-7.0.1(snapshot), and I get the same error with parameters -std=c++11, -std=c++14, -std=c++17.

+4

Source: https://habr.com/ru/post/1669150/


All Articles