int f(auto obj) {
if constexpr (HasFastGetData<decltype(obj)>) {
return obj.FastGetData();
} else {
return obj.GetData();
}
}
int main() {
B obj;
f(obj);
// How to verify obj.FastGetData(), rather than obj.GetData(), is executed?
}
Take the code above as an example:
- I have two classes
A&B. Ahas a member functionint GetData()andBhas a member functionint FastGetData().- The two functions have the same symantics, but
FastGetDatais faster thanGetData. - I want
fto differentiate the types of obj for better performance.
I just wonder:
Is there an effective way to unit test my intent?
You can either add
static_assert(HasFastGetData<B>)after the call tofor return astd::paircontaining the result:Method 1
Method 2
You can also return a
std::pair(or a custom struct with sensible named members) to check: