问题
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