Can the return type of the function be obtained from within the function?
Clash Royale CLAN TAG#URR8PPP
up vote
31
down vote
favorite
Can the return type of a function be obtained in a simple way within the function?
For example, given:
template <typename P>
static inline auto foo(P p) -> typename std::remove_reference<decltype(*p)>::type
typename std::remove_reference<decltype(*p)>::type f; // <-- here
...
In C++11 can I refer to the big nasty return type of foo
, within foo
itself, without repeating it, at the line marked // <-- here
?
c++ c++11 trailing-return-type
add a comment |Â
up vote
31
down vote
favorite
Can the return type of a function be obtained in a simple way within the function?
For example, given:
template <typename P>
static inline auto foo(P p) -> typename std::remove_reference<decltype(*p)>::type
typename std::remove_reference<decltype(*p)>::type f; // <-- here
...
In C++11 can I refer to the big nasty return type of foo
, within foo
itself, without repeating it, at the line marked // <-- here
?
c++ c++11 trailing-return-type
1
In C++14, you can remove the trailing return type instead, and usereturn f;
to deduce the return type.
– jingyu9575
Sep 6 at 4:42
add a comment |Â
up vote
31
down vote
favorite
up vote
31
down vote
favorite
Can the return type of a function be obtained in a simple way within the function?
For example, given:
template <typename P>
static inline auto foo(P p) -> typename std::remove_reference<decltype(*p)>::type
typename std::remove_reference<decltype(*p)>::type f; // <-- here
...
In C++11 can I refer to the big nasty return type of foo
, within foo
itself, without repeating it, at the line marked // <-- here
?
c++ c++11 trailing-return-type
Can the return type of a function be obtained in a simple way within the function?
For example, given:
template <typename P>
static inline auto foo(P p) -> typename std::remove_reference<decltype(*p)>::type
typename std::remove_reference<decltype(*p)>::type f; // <-- here
...
In C++11 can I refer to the big nasty return type of foo
, within foo
itself, without repeating it, at the line marked // <-- here
?
c++ c++11 trailing-return-type
edited Sep 5 at 18:24
asked Sep 5 at 17:33
BeeOnRope
23.7k872160
23.7k872160
1
In C++14, you can remove the trailing return type instead, and usereturn f;
to deduce the return type.
– jingyu9575
Sep 6 at 4:42
add a comment |Â
1
In C++14, you can remove the trailing return type instead, and usereturn f;
to deduce the return type.
– jingyu9575
Sep 6 at 4:42
1
1
In C++14, you can remove the trailing return type instead, and use
return f;
to deduce the return type.– jingyu9575
Sep 6 at 4:42
In C++14, you can remove the trailing return type instead, and use
return f;
to deduce the return type.– jingyu9575
Sep 6 at 4:42
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
46
down vote
accepted
Call the function with a decltype
.
decltype(foo(p)) f;
5
life made easy bydecltype
;)
– JeJo
Sep 5 at 18:18
Works well in this case where there are few parameters...
– Max Langhof
Sep 6 at 8:01
@MaxLanghof Yes. The more general solution would be to define an alias and use that. :)
– Rakete1111
Sep 6 at 8:02
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
46
down vote
accepted
Call the function with a decltype
.
decltype(foo(p)) f;
5
life made easy bydecltype
;)
– JeJo
Sep 5 at 18:18
Works well in this case where there are few parameters...
– Max Langhof
Sep 6 at 8:01
@MaxLanghof Yes. The more general solution would be to define an alias and use that. :)
– Rakete1111
Sep 6 at 8:02
add a comment |Â
up vote
46
down vote
accepted
Call the function with a decltype
.
decltype(foo(p)) f;
5
life made easy bydecltype
;)
– JeJo
Sep 5 at 18:18
Works well in this case where there are few parameters...
– Max Langhof
Sep 6 at 8:01
@MaxLanghof Yes. The more general solution would be to define an alias and use that. :)
– Rakete1111
Sep 6 at 8:02
add a comment |Â
up vote
46
down vote
accepted
up vote
46
down vote
accepted
Call the function with a decltype
.
decltype(foo(p)) f;
Call the function with a decltype
.
decltype(foo(p)) f;
answered Sep 5 at 17:35


Rakete1111
31.9k973107
31.9k973107
5
life made easy bydecltype
;)
– JeJo
Sep 5 at 18:18
Works well in this case where there are few parameters...
– Max Langhof
Sep 6 at 8:01
@MaxLanghof Yes. The more general solution would be to define an alias and use that. :)
– Rakete1111
Sep 6 at 8:02
add a comment |Â
5
life made easy bydecltype
;)
– JeJo
Sep 5 at 18:18
Works well in this case where there are few parameters...
– Max Langhof
Sep 6 at 8:01
@MaxLanghof Yes. The more general solution would be to define an alias and use that. :)
– Rakete1111
Sep 6 at 8:02
5
5
life made easy by
decltype
;)– JeJo
Sep 5 at 18:18
life made easy by
decltype
;)– JeJo
Sep 5 at 18:18
Works well in this case where there are few parameters...
– Max Langhof
Sep 6 at 8:01
Works well in this case where there are few parameters...
– Max Langhof
Sep 6 at 8:01
@MaxLanghof Yes. The more general solution would be to define an alias and use that. :)
– Rakete1111
Sep 6 at 8:02
@MaxLanghof Yes. The more general solution would be to define an alias and use that. :)
– Rakete1111
Sep 6 at 8:02
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52190625%2fcan-the-return-type-of-the-function-be-obtained-from-within-the-function%23new-answer', 'question_page');
);
Post as a guest
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
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
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
1
In C++14, you can remove the trailing return type instead, and use
return f;
to deduce the return type.– jingyu9575
Sep 6 at 4:42