Assume we have:
template <typename T>
void foo(std::vector<T> &t) {
auto var = T();
std::vector<decltype(var.Get())> array;
}
In the above code, an array is created. The type of this array is whatever the return value of Get() is. We find this value by creating a dummy variable of type T and then infering the return type of Get by using decltype.
This works, however it requires the creation of a dummy variable that serves no purpose.
Instead we could do:
template <typename T>
void foo(std::vector<T> &t) {
auto var = t[0];
std::vector<decltype(var.Get())> array;
}
Which doesn't create any dummies, but this will potentially crash, given that we have no guarantee that the array contains at least on element.
Is there a way to infer the type of .Get() without creating a dummy?
std::vector<decltype(t[0].Get())>
t[0]
will not be invoked as decltype
is an unevaluated context.
Alternatives:
std::vector<decltype(T().Get())>
std::vector<decltype(std::declval<T&>().Get())>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With