Is it possible to deduce template value (not type) for a c++17 function?
The function foo:
template<int I>
int foo()
{
return (I);
}
Can be called via:
foo<5>();
And will return 5.
Template types can be deduced through the type of a function argument. Is it possible to do the same in some way for the template value? For example:
template<int I = x>
int bar(const int x)
{
return (I);
}
This obviously wont work (because for one x
is required before its declaration), but might there be some C++17 trick which would allow for this?
I'd like to use this as a way of setting a constant expression function argument.
What you want can only be done by (ab)using type deduction for integer deduction. Observe:
template<int x>
struct integer_value {};
template<int x>
void test(integer_value<x> val)
{
//x can be used here.
}
Of course, you must invoke this with test(integer_value<4>{})
or something similar.