TMP, discover whether the return type of a callable is boolean

Hi there,
how can we detect the return type of a callable? std::invoke_result requires to specify the complete signature, which I don't know.

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

template <typename Callback, typename = void>
struct is_return_value_bool : std::false_type { };

template <typename Callback>
struct is_return_value_bool<Callback, what here ? > : std::true_type { };

int main(int argc, const char* argv[]){
    auto f = [](int) { return; };
    auto g = [](int, int) { return true; };

    cout << "f: " << is_return_value_bool<decltype(f)>::value << endl; // false
    cout << "g: " << is_return_value_bool<decltype(g)>::value << endl; // true

    return 0;
}
Last edited on
Have a look at std::is_same for comparing types.

https://en.cppreference.com/w/cpp/types/is_same
http://www.cplusplus.com/reference/type_traits/is_same/

1
2
3
4
5
6
7
8
9
10
#include <type_traits>
#include <iostream>

int main() {
	auto f = []() { return; };
	auto g = []() { return true; };

	std::cout << "f: " << std::is_same<bool, decltype(f())>::value << '\n'; // false
	std::cout << "g: " << std::is_same<bool, decltype(g())> ::value << '\n'; // true
}


Note that decltype(f) or decltype(g) won't return the type you think. It will return the type of the f or g lambda - nor the return type of the lambda.


f: 0
g: 1

Last edited on
@dean,

This topic is far from beginner material.

I think you could certainly be posting in the "General C++ Programming" forum.
Hey everyone:

I think I managed to find a solution as :

1
2
3
4
5
6
7
8
9
10
// based on https://stackoverflow.com/questions/53673442/simplest-way-to-determine-return-type-of-function
template <typename Callback>
using return_type_t = typename decltype(std::function{declval<Callback>()})::result_type;

template <typename Callback, typename = void>
struct is_return_value_bool : std::false_type { };

template <typename Callback>
struct is_return_value_bool<Callback, std::enable_if_t< std::is_same_v<return_type_t<Callback>, bool> > > : std::true_type { };


But please let me know if there's a simpler or more idiomatic way. I'm still moving my first steps with TMP xD

@doug4 I still feel a beginner :P. How can I move the thread to the other forum?
You cannot determine the return type of all Callable or indeed even all FunctionObjects, without a choice of arguments used to invoke them. Therefore, let's step back a bit.

Why do you think you need this information? What problem are you trying to solve?
Last edited on
That's a clever solution, using std::function and declval.

The rest can be shortened a bit by using std::is_same<bool, return_type_t<Callback>>::value.

-Albatross
How can I move the thread to the other forum?


Don't move this one. Future threads probably belong in the the general forum.

I can just imagine a newby poking around on the beginner's forum and seeing a question involving:

- template structs
- default template arguments
- std::true and std::false types
- lambda expressions
- decltype

In my mind, a beginner would be focused more on control structures, function calls, organizing data into classes, projects with multiple files, using std::vector rather than arrays, etc. Your topics might overwhelm a true beginner.

I still feel a beginner :P.

You're not. We all feel we have much to learn, but your questions belie your experience.
Registered users can post here. Sign in or register to post.