Reputation: 1580
The following function is to return the signed version of its parameter. How would I do that with just one template parameter?
template<class UINT_TYPE>
INT_TYPE function_x(const UINT_TYPE n)
{
static_assert(std::is_same<UINT_TYPE, uint32_t>::value ||
std::is_same<UINT_TYPE, uint64_t>::value,
"Wrong type passed.");
return static_cast<INT_TYPE>(n);
}
It is possible to use two template parameters with a default value but this is not preferred.
template<class UINT_TYPE, class INT_TYPE = typename std::make_signed<UINT_TYPE>::type>
If there is any way to do this neatly your ideas are appreciated.
Upvotes: 0
Views: 72
Reputation: 119587
You can deduce the return type:
template <class UINT_TYPE>
auto function_x(const UINT_TYPE n) {
// ...
return static_cast<std::make_signed_t<UINT_TYPE>>(n);
}
Upvotes: 4