Can the return type of the function be obtained from within the function?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
38
down vote

favorite
3












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?










share|improve this question



















  • 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














up vote
38
down vote

favorite
3












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?










share|improve this question



















  • 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












up vote
38
down vote

favorite
3









up vote
38
down vote

favorite
3






3





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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 5 at 18:24

























asked Sep 5 at 17:33









BeeOnRope

24k873162




24k873162







  • 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












  • 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







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












1 Answer
1






active

oldest

votes

















up vote
50
down vote



accepted










Call the function with a decltype.



decltype(foo(p)) f;





share|improve this answer
















  • 6




    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










  • @MaxLanghof Yes. The more general solution would be to define an alias and use that. :)
    – Rakete1111
    Sep 6 at 8:02










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: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
50
down vote



accepted










Call the function with a decltype.



decltype(foo(p)) f;





share|improve this answer
















  • 6




    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










  • @MaxLanghof Yes. The more general solution would be to define an alias and use that. :)
    – Rakete1111
    Sep 6 at 8:02














up vote
50
down vote



accepted










Call the function with a decltype.



decltype(foo(p)) f;





share|improve this answer
















  • 6




    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










  • @MaxLanghof Yes. The more general solution would be to define an alias and use that. :)
    – Rakete1111
    Sep 6 at 8:02












up vote
50
down vote



accepted







up vote
50
down vote



accepted






Call the function with a decltype.



decltype(foo(p)) f;





share|improve this answer












Call the function with a decltype.



decltype(foo(p)) f;






share|improve this answer












share|improve this answer



share|improve this answer










answered Sep 5 at 17:35









Rakete1111

32.7k975110




32.7k975110







  • 6




    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










  • @MaxLanghof Yes. The more general solution would be to define an alias and use that. :)
    – Rakete1111
    Sep 6 at 8:02












  • 6




    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










  • @MaxLanghof Yes. The more general solution would be to define an alias and use that. :)
    – Rakete1111
    Sep 6 at 8:02







6




6




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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay