invoke_result
Deduces the return type of the expression passed. This deduction happens at compile time.
template < class F, class... ArgTypes >
using invoke_result_t = typename invoke_result <F, ArgTypes...>::type;
Code
#include <type_traits>
std::string intToString(int x) {
return std::to_string(x);
}
int main() {
// 1. Get the return type at compile-time
using MyReturnType = std::invoke_result_t <decltype(intToString), int>
// 2. Ask the compiler: "Is MyReturnType the same as std::string?"
// This check happens instantly while compiling, not while running!
if constexpr (std::is_same_v < MyReturnType, std::string>) {
std::cout << "Return type is string!" << std::endl;
} else {
std::cout << "Return type is something else." << std::endl;
}
return 0;
}