Reputation: 3336
quick question. If I have a function signature like
template <typename T, typename ItType>
ItType binarySearch ( T mid, ItType first, ItType last );
Is there anyway to do something like the following? I know this syntax isn't correct but you get the idea as I can do a decltype similar with regular functions see below. The compiler knows the type of ItType at compile time so shouldn't it be able to deduce the type of *ItType as well?
template <typename ItType>
ItType binarySearch ( decltype(*ItType) mid, ItType first, ItType last );
// lambda
auto p = v.begin() + (v.end() - v.begin())/2;
std::partition ( v.begin(), v.end(), [p](decltype(*p) i) { return i < *p; } )
Upvotes: 3
Views: 1168
Reputation: 56921
The problem with
decltype(*ItType)
is that *ItType
is not a valid expression. A naive approach could look like this:
decltype(*ItType())
which would work if ItType
is default constructible. Since you don't want to enforce that, you can use std::declval
to "call" a function that pretends to return an instance of ItType
:
decltype(*std::declval<ItType>())
This function is only declared but never defined which means you can not really call it, but that doesn't matter since you are using it within decltype()
, which is an unevaluated context.
Upvotes: 1