Is it possible to use function return type as an argument in declaration of another function in C++?

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











up vote
23
down vote

favorite












I want something like this:



std::tuple<int, bool, double> MyFunction_1 (void);

void MyFunction_2 (decltype (MyFunction_1) &params);


Obviously, in this example a code pointer to function would be passed.



I want to have the equivalent of this:



void MyFunction_2 (std::tuple<int, bool, double> &params);


Is it possible to do so?










share|improve this question



















  • 1




    About wording, I think you are asking if it is possible to use MyFunction_1 return type as an argument for MyFunction_2
    – Loreto
    1 hour ago










  • Yup. Thanks for correction
    – Dmitrii Motorygin
    38 mins ago














up vote
23
down vote

favorite












I want something like this:



std::tuple<int, bool, double> MyFunction_1 (void);

void MyFunction_2 (decltype (MyFunction_1) &params);


Obviously, in this example a code pointer to function would be passed.



I want to have the equivalent of this:



void MyFunction_2 (std::tuple<int, bool, double> &params);


Is it possible to do so?










share|improve this question



















  • 1




    About wording, I think you are asking if it is possible to use MyFunction_1 return type as an argument for MyFunction_2
    – Loreto
    1 hour ago










  • Yup. Thanks for correction
    – Dmitrii Motorygin
    38 mins ago












up vote
23
down vote

favorite









up vote
23
down vote

favorite











I want something like this:



std::tuple<int, bool, double> MyFunction_1 (void);

void MyFunction_2 (decltype (MyFunction_1) &params);


Obviously, in this example a code pointer to function would be passed.



I want to have the equivalent of this:



void MyFunction_2 (std::tuple<int, bool, double> &params);


Is it possible to do so?










share|improve this question















I want something like this:



std::tuple<int, bool, double> MyFunction_1 (void);

void MyFunction_2 (decltype (MyFunction_1) &params);


Obviously, in this example a code pointer to function would be passed.



I want to have the equivalent of this:



void MyFunction_2 (std::tuple<int, bool, double> &params);


Is it possible to do so?







c++ c++11 decltype






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 37 mins ago

























asked 12 hours ago









Dmitrii Motorygin

1357




1357







  • 1




    About wording, I think you are asking if it is possible to use MyFunction_1 return type as an argument for MyFunction_2
    – Loreto
    1 hour ago










  • Yup. Thanks for correction
    – Dmitrii Motorygin
    38 mins ago












  • 1




    About wording, I think you are asking if it is possible to use MyFunction_1 return type as an argument for MyFunction_2
    – Loreto
    1 hour ago










  • Yup. Thanks for correction
    – Dmitrii Motorygin
    38 mins ago







1




1




About wording, I think you are asking if it is possible to use MyFunction_1 return type as an argument for MyFunction_2
– Loreto
1 hour ago




About wording, I think you are asking if it is possible to use MyFunction_1 return type as an argument for MyFunction_2
– Loreto
1 hour ago












Yup. Thanks for correction
– Dmitrii Motorygin
38 mins ago




Yup. Thanks for correction
– Dmitrii Motorygin
38 mins ago












2 Answers
2






active

oldest

votes

















up vote
29
down vote













decltype (MyFunction_1) will give you the type of MyFunction_1 (i.e. the function type std::tuple<int, bool, double> ()), you need to emulate a function calling 1 (via adding ()) to get the return type (i.e. std::tuple<int, bool, double>), e.g.



void MyFunction_2 (decltype (MyFunction_1()) &params);
// ^^



1 The expression is evaluated at compile-time, the function won't be called at run-time actually.






share|improve this answer


















  • 2




    Because....? FGITWing is nice and all, but an answer should explain to the OP and future readers why adding two characters solves the problem here.
    – StoryTeller
    12 hours ago






  • 2




    Would "you need to emulate a function call in the decltype()" be a better wording? :P
    – Rerito
    12 hours ago






  • 9




    Another approach (maybe more straightforward): std::result_of
    – Ptaq666
    12 hours ago






  • 5




    @Ptaq666 That's an answer and should be posted as one.
    – pipe
    9 hours ago

















up vote
15
down vote













The type of MyFunction_1 is not std::tuple<int, bool, double> - informally you can think of it as a function pointer. In fact &MyFunction_1 decays to itself and is certainly a pointer.



So decltype(MyFunction_1) is not what you want.



The solution is to write decltype(MyFunction_1()). The type of MyFunction_1() is std::tuple<int, bool, double>. Note that this doesn't actually call the function; it's rather like sizeof in that respect.






share|improve this answer
















  • 1




    MyFunction_1 decays to a pointer, &MyFunction_1 does not -- it is one already :)
    – Quentin
    7 hours ago










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%2f52811378%2fis-it-possible-to-use-function-return-type-as-an-argument-in-declaration-of-anot%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
29
down vote













decltype (MyFunction_1) will give you the type of MyFunction_1 (i.e. the function type std::tuple<int, bool, double> ()), you need to emulate a function calling 1 (via adding ()) to get the return type (i.e. std::tuple<int, bool, double>), e.g.



void MyFunction_2 (decltype (MyFunction_1()) &params);
// ^^



1 The expression is evaluated at compile-time, the function won't be called at run-time actually.






share|improve this answer


















  • 2




    Because....? FGITWing is nice and all, but an answer should explain to the OP and future readers why adding two characters solves the problem here.
    – StoryTeller
    12 hours ago






  • 2




    Would "you need to emulate a function call in the decltype()" be a better wording? :P
    – Rerito
    12 hours ago






  • 9




    Another approach (maybe more straightforward): std::result_of
    – Ptaq666
    12 hours ago






  • 5




    @Ptaq666 That's an answer and should be posted as one.
    – pipe
    9 hours ago














up vote
29
down vote













decltype (MyFunction_1) will give you the type of MyFunction_1 (i.e. the function type std::tuple<int, bool, double> ()), you need to emulate a function calling 1 (via adding ()) to get the return type (i.e. std::tuple<int, bool, double>), e.g.



void MyFunction_2 (decltype (MyFunction_1()) &params);
// ^^



1 The expression is evaluated at compile-time, the function won't be called at run-time actually.






share|improve this answer


















  • 2




    Because....? FGITWing is nice and all, but an answer should explain to the OP and future readers why adding two characters solves the problem here.
    – StoryTeller
    12 hours ago






  • 2




    Would "you need to emulate a function call in the decltype()" be a better wording? :P
    – Rerito
    12 hours ago






  • 9




    Another approach (maybe more straightforward): std::result_of
    – Ptaq666
    12 hours ago






  • 5




    @Ptaq666 That's an answer and should be posted as one.
    – pipe
    9 hours ago












up vote
29
down vote










up vote
29
down vote









decltype (MyFunction_1) will give you the type of MyFunction_1 (i.e. the function type std::tuple<int, bool, double> ()), you need to emulate a function calling 1 (via adding ()) to get the return type (i.e. std::tuple<int, bool, double>), e.g.



void MyFunction_2 (decltype (MyFunction_1()) &params);
// ^^



1 The expression is evaluated at compile-time, the function won't be called at run-time actually.






share|improve this answer














decltype (MyFunction_1) will give you the type of MyFunction_1 (i.e. the function type std::tuple<int, bool, double> ()), you need to emulate a function calling 1 (via adding ()) to get the return type (i.e. std::tuple<int, bool, double>), e.g.



void MyFunction_2 (decltype (MyFunction_1()) &params);
// ^^



1 The expression is evaluated at compile-time, the function won't be called at run-time actually.







share|improve this answer














share|improve this answer



share|improve this answer








edited 11 hours ago

























answered 12 hours ago









songyuanyao

86k9166226




86k9166226







  • 2




    Because....? FGITWing is nice and all, but an answer should explain to the OP and future readers why adding two characters solves the problem here.
    – StoryTeller
    12 hours ago






  • 2




    Would "you need to emulate a function call in the decltype()" be a better wording? :P
    – Rerito
    12 hours ago






  • 9




    Another approach (maybe more straightforward): std::result_of
    – Ptaq666
    12 hours ago






  • 5




    @Ptaq666 That's an answer and should be posted as one.
    – pipe
    9 hours ago












  • 2




    Because....? FGITWing is nice and all, but an answer should explain to the OP and future readers why adding two characters solves the problem here.
    – StoryTeller
    12 hours ago






  • 2




    Would "you need to emulate a function call in the decltype()" be a better wording? :P
    – Rerito
    12 hours ago






  • 9




    Another approach (maybe more straightforward): std::result_of
    – Ptaq666
    12 hours ago






  • 5




    @Ptaq666 That's an answer and should be posted as one.
    – pipe
    9 hours ago







2




2




Because....? FGITWing is nice and all, but an answer should explain to the OP and future readers why adding two characters solves the problem here.
– StoryTeller
12 hours ago




Because....? FGITWing is nice and all, but an answer should explain to the OP and future readers why adding two characters solves the problem here.
– StoryTeller
12 hours ago




2




2




Would "you need to emulate a function call in the decltype()" be a better wording? :P
– Rerito
12 hours ago




Would "you need to emulate a function call in the decltype()" be a better wording? :P
– Rerito
12 hours ago




9




9




Another approach (maybe more straightforward): std::result_of
– Ptaq666
12 hours ago




Another approach (maybe more straightforward): std::result_of
– Ptaq666
12 hours ago




5




5




@Ptaq666 That's an answer and should be posted as one.
– pipe
9 hours ago




@Ptaq666 That's an answer and should be posted as one.
– pipe
9 hours ago












up vote
15
down vote













The type of MyFunction_1 is not std::tuple<int, bool, double> - informally you can think of it as a function pointer. In fact &MyFunction_1 decays to itself and is certainly a pointer.



So decltype(MyFunction_1) is not what you want.



The solution is to write decltype(MyFunction_1()). The type of MyFunction_1() is std::tuple<int, bool, double>. Note that this doesn't actually call the function; it's rather like sizeof in that respect.






share|improve this answer
















  • 1




    MyFunction_1 decays to a pointer, &MyFunction_1 does not -- it is one already :)
    – Quentin
    7 hours ago














up vote
15
down vote













The type of MyFunction_1 is not std::tuple<int, bool, double> - informally you can think of it as a function pointer. In fact &MyFunction_1 decays to itself and is certainly a pointer.



So decltype(MyFunction_1) is not what you want.



The solution is to write decltype(MyFunction_1()). The type of MyFunction_1() is std::tuple<int, bool, double>. Note that this doesn't actually call the function; it's rather like sizeof in that respect.






share|improve this answer
















  • 1




    MyFunction_1 decays to a pointer, &MyFunction_1 does not -- it is one already :)
    – Quentin
    7 hours ago












up vote
15
down vote










up vote
15
down vote









The type of MyFunction_1 is not std::tuple<int, bool, double> - informally you can think of it as a function pointer. In fact &MyFunction_1 decays to itself and is certainly a pointer.



So decltype(MyFunction_1) is not what you want.



The solution is to write decltype(MyFunction_1()). The type of MyFunction_1() is std::tuple<int, bool, double>. Note that this doesn't actually call the function; it's rather like sizeof in that respect.






share|improve this answer












The type of MyFunction_1 is not std::tuple<int, bool, double> - informally you can think of it as a function pointer. In fact &MyFunction_1 decays to itself and is certainly a pointer.



So decltype(MyFunction_1) is not what you want.



The solution is to write decltype(MyFunction_1()). The type of MyFunction_1() is std::tuple<int, bool, double>. Note that this doesn't actually call the function; it's rather like sizeof in that respect.







share|improve this answer












share|improve this answer



share|improve this answer










answered 12 hours ago









Bathsheba

170k26239362




170k26239362







  • 1




    MyFunction_1 decays to a pointer, &MyFunction_1 does not -- it is one already :)
    – Quentin
    7 hours ago












  • 1




    MyFunction_1 decays to a pointer, &MyFunction_1 does not -- it is one already :)
    – Quentin
    7 hours ago







1




1




MyFunction_1 decays to a pointer, &MyFunction_1 does not -- it is one already :)
– Quentin
7 hours ago




MyFunction_1 decays to a pointer, &MyFunction_1 does not -- it is one already :)
– Quentin
7 hours ago

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52811378%2fis-it-possible-to-use-function-return-type-as-an-argument-in-declaration-of-anot%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

Long meetings (6-7 hours a day): Being “babysat” by supervisor

Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

Confectionery