Is it possible to use function return type as an argument in declaration of another function in C++?
Clash 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) ¶ms);
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> ¶ms);
Is it possible to do so?
c++ c++11 decltype
add a comment |Â
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) ¶ms);
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> ¶ms);
Is it possible to do so?
c++ c++11 decltype
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
add a comment |Â
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) ¶ms);
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> ¶ms);
Is it possible to do so?
c++ c++11 decltype
I want something like this:
std::tuple<int, bool, double> MyFunction_1 (void);
void MyFunction_2 (decltype (MyFunction_1) ¶ms);
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> ¶ms);
Is it possible to do so?
c++ c++11 decltype
c++ c++11 decltype
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
add a comment |Â
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
add a comment |Â
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()) ¶ms);
// ^^
1 The expression is evaluated at compile-time, the function won't be called at run-time actually.
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 thedecltype()
" 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
add a comment |Â
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.
1
MyFunction_1
decays to a pointer,&MyFunction_1
does not -- it is one already :)
â Quentin
7 hours ago
add a comment |Â
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()) ¶ms);
// ^^
1 The expression is evaluated at compile-time, the function won't be called at run-time actually.
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 thedecltype()
" 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
add a comment |Â
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()) ¶ms);
// ^^
1 The expression is evaluated at compile-time, the function won't be called at run-time actually.
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 thedecltype()
" 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
add a comment |Â
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()) ¶ms);
// ^^
1 The expression is evaluated at compile-time, the function won't be called at run-time actually.
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()) ¶ms);
// ^^
1 The expression is evaluated at compile-time, the function won't be called at run-time actually.
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 thedecltype()
" 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
add a comment |Â
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 thedecltype()
" 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
add a comment |Â
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.
1
MyFunction_1
decays to a pointer,&MyFunction_1
does not -- it is one already :)
â Quentin
7 hours ago
add a comment |Â
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.
1
MyFunction_1
decays to a pointer,&MyFunction_1
does not -- it is one already :)
â Quentin
7 hours ago
add a comment |Â
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.
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.
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
add a comment |Â
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
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%2f52811378%2fis-it-possible-to-use-function-return-type-as-an-argument-in-declaration-of-anot%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
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