Simplest way to determine return type of function
up vote
40
down vote
favorite
Given a very simple, but lengthy function, such as:
int foo(int a, int b, int c, int d) {
return 1;
}
// using ReturnTypeOfFoo = ???
What is the most simple and concise way to determine the function's return type (ReturnTypeOfFoo
, in this example: int
) at compile time without repeating the function's parameter types (by name only, since it is known that the function does not have any additional overloads)?
c++ function c++17 return-type compile-time
|
show 3 more comments
up vote
40
down vote
favorite
Given a very simple, but lengthy function, such as:
int foo(int a, int b, int c, int d) {
return 1;
}
// using ReturnTypeOfFoo = ???
What is the most simple and concise way to determine the function's return type (ReturnTypeOfFoo
, in this example: int
) at compile time without repeating the function's parameter types (by name only, since it is known that the function does not have any additional overloads)?
c++ function c++17 return-type compile-time
Depending on the compiler or the support libraries available to you... docs.microsoft.com/en-us/cpp/cpp/attributes?view=vs-2017, Fairly sure that Concepts also has some APIs which allow you to deduce this information at run time or compile time.
– Jay
Dec 7 at 16:33
1
Maybedecltype(foo)::result_type
?
– Thomas Lang
Dec 7 at 16:33
maybe en.cppreference.com/w/cpp/types/result_of?
– bracco23
Dec 7 at 16:35
1
@ThomasLang where does this come from?decltype(foo)
is a function that has noresult_type
member, or do I miss something?
– user463035818
Dec 7 at 16:39
@user463035818 You may be right here, I was referring to theresult_type
member of astd::function
type.
– Thomas Lang
Dec 7 at 16:40
|
show 3 more comments
up vote
40
down vote
favorite
up vote
40
down vote
favorite
Given a very simple, but lengthy function, such as:
int foo(int a, int b, int c, int d) {
return 1;
}
// using ReturnTypeOfFoo = ???
What is the most simple and concise way to determine the function's return type (ReturnTypeOfFoo
, in this example: int
) at compile time without repeating the function's parameter types (by name only, since it is known that the function does not have any additional overloads)?
c++ function c++17 return-type compile-time
Given a very simple, but lengthy function, such as:
int foo(int a, int b, int c, int d) {
return 1;
}
// using ReturnTypeOfFoo = ???
What is the most simple and concise way to determine the function's return type (ReturnTypeOfFoo
, in this example: int
) at compile time without repeating the function's parameter types (by name only, since it is known that the function does not have any additional overloads)?
c++ function c++17 return-type compile-time
c++ function c++17 return-type compile-time
edited Dec 7 at 17:44
Konrad Rudolph
393k1017731022
393k1017731022
asked Dec 7 at 16:29
Cybran
965717
965717
Depending on the compiler or the support libraries available to you... docs.microsoft.com/en-us/cpp/cpp/attributes?view=vs-2017, Fairly sure that Concepts also has some APIs which allow you to deduce this information at run time or compile time.
– Jay
Dec 7 at 16:33
1
Maybedecltype(foo)::result_type
?
– Thomas Lang
Dec 7 at 16:33
maybe en.cppreference.com/w/cpp/types/result_of?
– bracco23
Dec 7 at 16:35
1
@ThomasLang where does this come from?decltype(foo)
is a function that has noresult_type
member, or do I miss something?
– user463035818
Dec 7 at 16:39
@user463035818 You may be right here, I was referring to theresult_type
member of astd::function
type.
– Thomas Lang
Dec 7 at 16:40
|
show 3 more comments
Depending on the compiler or the support libraries available to you... docs.microsoft.com/en-us/cpp/cpp/attributes?view=vs-2017, Fairly sure that Concepts also has some APIs which allow you to deduce this information at run time or compile time.
– Jay
Dec 7 at 16:33
1
Maybedecltype(foo)::result_type
?
– Thomas Lang
Dec 7 at 16:33
maybe en.cppreference.com/w/cpp/types/result_of?
– bracco23
Dec 7 at 16:35
1
@ThomasLang where does this come from?decltype(foo)
is a function that has noresult_type
member, or do I miss something?
– user463035818
Dec 7 at 16:39
@user463035818 You may be right here, I was referring to theresult_type
member of astd::function
type.
– Thomas Lang
Dec 7 at 16:40
Depending on the compiler or the support libraries available to you... docs.microsoft.com/en-us/cpp/cpp/attributes?view=vs-2017, Fairly sure that Concepts also has some APIs which allow you to deduce this information at run time or compile time.
– Jay
Dec 7 at 16:33
Depending on the compiler or the support libraries available to you... docs.microsoft.com/en-us/cpp/cpp/attributes?view=vs-2017, Fairly sure that Concepts also has some APIs which allow you to deduce this information at run time or compile time.
– Jay
Dec 7 at 16:33
1
1
Maybe
decltype(foo)::result_type
?– Thomas Lang
Dec 7 at 16:33
Maybe
decltype(foo)::result_type
?– Thomas Lang
Dec 7 at 16:33
maybe en.cppreference.com/w/cpp/types/result_of?
– bracco23
Dec 7 at 16:35
maybe en.cppreference.com/w/cpp/types/result_of?
– bracco23
Dec 7 at 16:35
1
1
@ThomasLang where does this come from?
decltype(foo)
is a function that has no result_type
member, or do I miss something?– user463035818
Dec 7 at 16:39
@ThomasLang where does this come from?
decltype(foo)
is a function that has no result_type
member, or do I miss something?– user463035818
Dec 7 at 16:39
@user463035818 You may be right here, I was referring to the
result_type
member of a std::function
type.– Thomas Lang
Dec 7 at 16:40
@user463035818 You may be right here, I was referring to the
result_type
member of a std::function
type.– Thomas Lang
Dec 7 at 16:40
|
show 3 more comments
3 Answers
3
active
oldest
votes
up vote
48
down vote
accepted
You can leverage std::function
here which will give you a typedef for the functions return type. This does require C++17 support, since it relies on class template argument deduction, but it will work with any callable type:
using ReturnTypeOfFoo = decltype(std::function{foo})::result_type;
We can make this a little more generic like
template<typename Callable>
using return_type_of_t =
typename decltype(std::function{std::declval<Callable>()})::result_type;
which then lets you use it like
int foo(int a, int b, int c, int d) {
return 1;
}
auto bar = (){ return 1; };
struct baz_
{
double operator()(){ return 0; }
} baz;
using ReturnTypeOfFoo = return_type_of_t<decltype(foo)>;
using ReturnTypeOfBar = return_type_of_t<decltype(bar)>;
using ReturnTypeOfBaz = return_type_of_t<decltype(baz)>;
Very compact! I can confirm the one-liner also works for template methods, as long as all template arguments specified.
– Cybran
Dec 7 at 19:08
add a comment |
up vote
19
down vote
Most simple and concise is probably:
template <typename R, typename... Args>
R return_type_of(R(*)(Args...));
using ReturnTypeOfFoo = decltype(return_type_of(foo));
Note that this won't work for function objects or pointers to member functions. Just functions, that aren't overloaded or templates, or noexcept
.
But this can be extended to support all of those cases, if so desired, by adding more overloads of return_type_of
.
add a comment |
up vote
13
down vote
I don't know if is the simplest way (if you can use C++17 surely isn't: see NathanOliver's answer) but... what about declaring a function as follows:
template <typename R, typename ... Args>
R getRetType (R(*)(Args...));
and using decltype()
?
using ReturnTypeOfFoo = decltype( getRetType(&foo) );
Observe that getRetType()
is only declared and not defined because is called only a decltype()
, so only the returned type is relevant.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53673442%2fsimplest-way-to-determine-return-type-of-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
48
down vote
accepted
You can leverage std::function
here which will give you a typedef for the functions return type. This does require C++17 support, since it relies on class template argument deduction, but it will work with any callable type:
using ReturnTypeOfFoo = decltype(std::function{foo})::result_type;
We can make this a little more generic like
template<typename Callable>
using return_type_of_t =
typename decltype(std::function{std::declval<Callable>()})::result_type;
which then lets you use it like
int foo(int a, int b, int c, int d) {
return 1;
}
auto bar = (){ return 1; };
struct baz_
{
double operator()(){ return 0; }
} baz;
using ReturnTypeOfFoo = return_type_of_t<decltype(foo)>;
using ReturnTypeOfBar = return_type_of_t<decltype(bar)>;
using ReturnTypeOfBaz = return_type_of_t<decltype(baz)>;
Very compact! I can confirm the one-liner also works for template methods, as long as all template arguments specified.
– Cybran
Dec 7 at 19:08
add a comment |
up vote
48
down vote
accepted
You can leverage std::function
here which will give you a typedef for the functions return type. This does require C++17 support, since it relies on class template argument deduction, but it will work with any callable type:
using ReturnTypeOfFoo = decltype(std::function{foo})::result_type;
We can make this a little more generic like
template<typename Callable>
using return_type_of_t =
typename decltype(std::function{std::declval<Callable>()})::result_type;
which then lets you use it like
int foo(int a, int b, int c, int d) {
return 1;
}
auto bar = (){ return 1; };
struct baz_
{
double operator()(){ return 0; }
} baz;
using ReturnTypeOfFoo = return_type_of_t<decltype(foo)>;
using ReturnTypeOfBar = return_type_of_t<decltype(bar)>;
using ReturnTypeOfBaz = return_type_of_t<decltype(baz)>;
Very compact! I can confirm the one-liner also works for template methods, as long as all template arguments specified.
– Cybran
Dec 7 at 19:08
add a comment |
up vote
48
down vote
accepted
up vote
48
down vote
accepted
You can leverage std::function
here which will give you a typedef for the functions return type. This does require C++17 support, since it relies on class template argument deduction, but it will work with any callable type:
using ReturnTypeOfFoo = decltype(std::function{foo})::result_type;
We can make this a little more generic like
template<typename Callable>
using return_type_of_t =
typename decltype(std::function{std::declval<Callable>()})::result_type;
which then lets you use it like
int foo(int a, int b, int c, int d) {
return 1;
}
auto bar = (){ return 1; };
struct baz_
{
double operator()(){ return 0; }
} baz;
using ReturnTypeOfFoo = return_type_of_t<decltype(foo)>;
using ReturnTypeOfBar = return_type_of_t<decltype(bar)>;
using ReturnTypeOfBaz = return_type_of_t<decltype(baz)>;
You can leverage std::function
here which will give you a typedef for the functions return type. This does require C++17 support, since it relies on class template argument deduction, but it will work with any callable type:
using ReturnTypeOfFoo = decltype(std::function{foo})::result_type;
We can make this a little more generic like
template<typename Callable>
using return_type_of_t =
typename decltype(std::function{std::declval<Callable>()})::result_type;
which then lets you use it like
int foo(int a, int b, int c, int d) {
return 1;
}
auto bar = (){ return 1; };
struct baz_
{
double operator()(){ return 0; }
} baz;
using ReturnTypeOfFoo = return_type_of_t<decltype(foo)>;
using ReturnTypeOfBar = return_type_of_t<decltype(bar)>;
using ReturnTypeOfBaz = return_type_of_t<decltype(baz)>;
edited Dec 8 at 8:50
coldspeed
114k18105182
114k18105182
answered Dec 7 at 16:44
NathanOliver
85.3k15118177
85.3k15118177
Very compact! I can confirm the one-liner also works for template methods, as long as all template arguments specified.
– Cybran
Dec 7 at 19:08
add a comment |
Very compact! I can confirm the one-liner also works for template methods, as long as all template arguments specified.
– Cybran
Dec 7 at 19:08
Very compact! I can confirm the one-liner also works for template methods, as long as all template arguments specified.
– Cybran
Dec 7 at 19:08
Very compact! I can confirm the one-liner also works for template methods, as long as all template arguments specified.
– Cybran
Dec 7 at 19:08
add a comment |
up vote
19
down vote
Most simple and concise is probably:
template <typename R, typename... Args>
R return_type_of(R(*)(Args...));
using ReturnTypeOfFoo = decltype(return_type_of(foo));
Note that this won't work for function objects or pointers to member functions. Just functions, that aren't overloaded or templates, or noexcept
.
But this can be extended to support all of those cases, if so desired, by adding more overloads of return_type_of
.
add a comment |
up vote
19
down vote
Most simple and concise is probably:
template <typename R, typename... Args>
R return_type_of(R(*)(Args...));
using ReturnTypeOfFoo = decltype(return_type_of(foo));
Note that this won't work for function objects or pointers to member functions. Just functions, that aren't overloaded or templates, or noexcept
.
But this can be extended to support all of those cases, if so desired, by adding more overloads of return_type_of
.
add a comment |
up vote
19
down vote
up vote
19
down vote
Most simple and concise is probably:
template <typename R, typename... Args>
R return_type_of(R(*)(Args...));
using ReturnTypeOfFoo = decltype(return_type_of(foo));
Note that this won't work for function objects or pointers to member functions. Just functions, that aren't overloaded or templates, or noexcept
.
But this can be extended to support all of those cases, if so desired, by adding more overloads of return_type_of
.
Most simple and concise is probably:
template <typename R, typename... Args>
R return_type_of(R(*)(Args...));
using ReturnTypeOfFoo = decltype(return_type_of(foo));
Note that this won't work for function objects or pointers to member functions. Just functions, that aren't overloaded or templates, or noexcept
.
But this can be extended to support all of those cases, if so desired, by adding more overloads of return_type_of
.
answered Dec 7 at 16:34
Barry
175k18299554
175k18299554
add a comment |
add a comment |
up vote
13
down vote
I don't know if is the simplest way (if you can use C++17 surely isn't: see NathanOliver's answer) but... what about declaring a function as follows:
template <typename R, typename ... Args>
R getRetType (R(*)(Args...));
and using decltype()
?
using ReturnTypeOfFoo = decltype( getRetType(&foo) );
Observe that getRetType()
is only declared and not defined because is called only a decltype()
, so only the returned type is relevant.
add a comment |
up vote
13
down vote
I don't know if is the simplest way (if you can use C++17 surely isn't: see NathanOliver's answer) but... what about declaring a function as follows:
template <typename R, typename ... Args>
R getRetType (R(*)(Args...));
and using decltype()
?
using ReturnTypeOfFoo = decltype( getRetType(&foo) );
Observe that getRetType()
is only declared and not defined because is called only a decltype()
, so only the returned type is relevant.
add a comment |
up vote
13
down vote
up vote
13
down vote
I don't know if is the simplest way (if you can use C++17 surely isn't: see NathanOliver's answer) but... what about declaring a function as follows:
template <typename R, typename ... Args>
R getRetType (R(*)(Args...));
and using decltype()
?
using ReturnTypeOfFoo = decltype( getRetType(&foo) );
Observe that getRetType()
is only declared and not defined because is called only a decltype()
, so only the returned type is relevant.
I don't know if is the simplest way (if you can use C++17 surely isn't: see NathanOliver's answer) but... what about declaring a function as follows:
template <typename R, typename ... Args>
R getRetType (R(*)(Args...));
and using decltype()
?
using ReturnTypeOfFoo = decltype( getRetType(&foo) );
Observe that getRetType()
is only declared and not defined because is called only a decltype()
, so only the returned type is relevant.
edited Dec 7 at 17:26
NathanOliver
85.3k15118177
85.3k15118177
answered Dec 7 at 16:34
max66
33.8k63762
33.8k63762
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53673442%2fsimplest-way-to-determine-return-type-of-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Depending on the compiler or the support libraries available to you... docs.microsoft.com/en-us/cpp/cpp/attributes?view=vs-2017, Fairly sure that Concepts also has some APIs which allow you to deduce this information at run time or compile time.
– Jay
Dec 7 at 16:33
1
Maybe
decltype(foo)::result_type
?– Thomas Lang
Dec 7 at 16:33
maybe en.cppreference.com/w/cpp/types/result_of?
– bracco23
Dec 7 at 16:35
1
@ThomasLang where does this come from?
decltype(foo)
is a function that has noresult_type
member, or do I miss something?– user463035818
Dec 7 at 16:39
@user463035818 You may be right here, I was referring to the
result_type
member of astd::function
type.– Thomas Lang
Dec 7 at 16:40