Can we have a C++ function with multiple return types? ( C++11 and above)Advantage of using trailing return type in C++11 functionsStoring C++ template function definitions in a .CPP fileWhat are POD types in C++?What should main() return in C and C++?How can I profile C++ code running on Linux?Why do we need virtual functions in C++?C++11 rvalues and move semantics confusion (return statement)C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?The std::transform-like function that returns transformed containerUsing a type that depends on lambda function as a return typeIs it possible to overload a template function based on whether the type is an integer type or a floating point type?
Why are there no programmes / playbills for movies?
Talk about Grandpa's weird talk: Who are these folks?
Cube around 2 points with correct perspective
How can I add a link on the "Structure" admin page?
How can I create folders in folders in terminal
Hobby function generators
Explanation of 申し訳ございません
Could the Orion project pusher plate model be used for asteroid deflection?
Do household ovens ventilate heat to the outdoors?
How far away from you does grass spread?
Why do we need to use transistors when building an OR gate?
All numbers in a 5x5 Minesweeper grid
Did slaves have slaves?
Exam design: give maximum score per question or not?
Do encumbered characters suffer any penalties when exploring and/or traveling?
Applications of mathematics in clinical setting
How is underwater propagation of sound possible?
What was the earliest microcomputer Logo language implementation?
Output Distinct Factor Cuboids
Which block header fields are miners able to change in an effort to avoid having to recalculate the Merkle Root?
How important is weather sealing for a winter trip?
Is Zack Morris's 'time stop' ability in "Saved By the Bell" a supernatural ability?
What is the source of "You can achieve a lot with hate, but even more with love" (Shakespeare?)
Is there sense in using const std::string& arguments in C++17?
Can we have a C++ function with multiple return types? ( C++11 and above)
Advantage of using trailing return type in C++11 functionsStoring C++ template function definitions in a .CPP fileWhat are POD types in C++?What should main() return in C and C++?How can I profile C++ code running on Linux?Why do we need virtual functions in C++?C++11 rvalues and move semantics confusion (return statement)C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?The std::transform-like function that returns transformed containerUsing a type that depends on lambda function as a return typeIs it possible to overload a template function based on whether the type is an integer type or a floating point type?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am trying to write a generic function which takes input as uint8
, uint16
, uint32
, uint64
, .... and return the maximum value with datatype of largest element?
For example:
template < typename T, typename X>
auto Max_Number ( T valueA, X valueB )
if ( valueA > valueB )
return valueA;
else
return valueB;
P.S: this example assumes the largest element is of largest datatype.
c++ c++11 templates return-type decltype
New contributor
add a comment
|
I am trying to write a generic function which takes input as uint8
, uint16
, uint32
, uint64
, .... and return the maximum value with datatype of largest element?
For example:
template < typename T, typename X>
auto Max_Number ( T valueA, X valueB )
if ( valueA > valueB )
return valueA;
else
return valueB;
P.S: this example assumes the largest element is of largest datatype.
c++ c++11 templates return-type decltype
New contributor
3
take a look atstd::variant
That's probably the closest since C++ is strongly typed
– doug
12 hours ago
There might be some template wizards that could suggest something, but I'm fairly certain that the return type must be fixed
– selbie
12 hours ago
4
Have you tried to use plainstd::max
? Remember that integer types can safely be implicitly converted to a larger type.
– Some programmer dude
12 hours ago
@Someprogrammerdude Yep, that should work given his example's statement that the largest arg magnitude is also the largest datatype applies in all other cases.
– doug
12 hours ago
Are the arguments always unsigned? It's not clear because of that "...". If they are, I would use simplyuintmax_t Max_Number(uintmax_t a, uintmax_t b) ...
.
– Daniel Langr
11 hours ago
add a comment
|
I am trying to write a generic function which takes input as uint8
, uint16
, uint32
, uint64
, .... and return the maximum value with datatype of largest element?
For example:
template < typename T, typename X>
auto Max_Number ( T valueA, X valueB )
if ( valueA > valueB )
return valueA;
else
return valueB;
P.S: this example assumes the largest element is of largest datatype.
c++ c++11 templates return-type decltype
New contributor
I am trying to write a generic function which takes input as uint8
, uint16
, uint32
, uint64
, .... and return the maximum value with datatype of largest element?
For example:
template < typename T, typename X>
auto Max_Number ( T valueA, X valueB )
if ( valueA > valueB )
return valueA;
else
return valueB;
P.S: this example assumes the largest element is of largest datatype.
c++ c++11 templates return-type decltype
c++ c++11 templates return-type decltype
New contributor
New contributor
edited 12 hours ago
Oblivion
2,5821 gold badge4 silver badges21 bronze badges
2,5821 gold badge4 silver badges21 bronze badges
New contributor
asked 12 hours ago
Kiran NDKiran ND
311 bronze badge
311 bronze badge
New contributor
New contributor
3
take a look atstd::variant
That's probably the closest since C++ is strongly typed
– doug
12 hours ago
There might be some template wizards that could suggest something, but I'm fairly certain that the return type must be fixed
– selbie
12 hours ago
4
Have you tried to use plainstd::max
? Remember that integer types can safely be implicitly converted to a larger type.
– Some programmer dude
12 hours ago
@Someprogrammerdude Yep, that should work given his example's statement that the largest arg magnitude is also the largest datatype applies in all other cases.
– doug
12 hours ago
Are the arguments always unsigned? It's not clear because of that "...". If they are, I would use simplyuintmax_t Max_Number(uintmax_t a, uintmax_t b) ...
.
– Daniel Langr
11 hours ago
add a comment
|
3
take a look atstd::variant
That's probably the closest since C++ is strongly typed
– doug
12 hours ago
There might be some template wizards that could suggest something, but I'm fairly certain that the return type must be fixed
– selbie
12 hours ago
4
Have you tried to use plainstd::max
? Remember that integer types can safely be implicitly converted to a larger type.
– Some programmer dude
12 hours ago
@Someprogrammerdude Yep, that should work given his example's statement that the largest arg magnitude is also the largest datatype applies in all other cases.
– doug
12 hours ago
Are the arguments always unsigned? It's not clear because of that "...". If they are, I would use simplyuintmax_t Max_Number(uintmax_t a, uintmax_t b) ...
.
– Daniel Langr
11 hours ago
3
3
take a look at
std::variant
That's probably the closest since C++ is strongly typed– doug
12 hours ago
take a look at
std::variant
That's probably the closest since C++ is strongly typed– doug
12 hours ago
There might be some template wizards that could suggest something, but I'm fairly certain that the return type must be fixed
– selbie
12 hours ago
There might be some template wizards that could suggest something, but I'm fairly certain that the return type must be fixed
– selbie
12 hours ago
4
4
Have you tried to use plain
std::max
? Remember that integer types can safely be implicitly converted to a larger type.– Some programmer dude
12 hours ago
Have you tried to use plain
std::max
? Remember that integer types can safely be implicitly converted to a larger type.– Some programmer dude
12 hours ago
@Someprogrammerdude Yep, that should work given his example's statement that the largest arg magnitude is also the largest datatype applies in all other cases.
– doug
12 hours ago
@Someprogrammerdude Yep, that should work given his example's statement that the largest arg magnitude is also the largest datatype applies in all other cases.
– doug
12 hours ago
Are the arguments always unsigned? It's not clear because of that "...". If they are, I would use simply
uintmax_t Max_Number(uintmax_t a, uintmax_t b) ...
.– Daniel Langr
11 hours ago
Are the arguments always unsigned? It's not clear because of that "...". If they are, I would use simply
uintmax_t Max_Number(uintmax_t a, uintmax_t b) ...
.– Daniel Langr
11 hours ago
add a comment
|
3 Answers
3
active
oldest
votes
The return type must be determined at compile-time. You might use std::common_type
(since C++11):
For arithmetic types not subject to promotion, the common type may be viewed as the type of the (possibly mixed-mode) arithmetic expression such as
T0() + T1() + ... + Tn()
.
template < typename T, typename X>
std::common_type_t<T, X> Max_Number ( T valueA, X valueB )
...
Or use std::conditional
(since C++11) to declare the return type as the big one (whose sizeof
is greater).
template < typename T, typename X>
std::conditional_t<sizeof(T) >= sizeof(X), T, X> Max_Number ( T valueA, X valueB )
...
add a comment
|
Here is an example solution with std:: variant
template < typename T, typename X>
std::variant<T, X> Max_Number ( T valueA, X valueB )
std::variant<T, X> res;
if ( valueA > valueB )
res = valueA;
else
res = valueB;
return res;
add a comment
|
The Trailing return with a conditional operator is another way to go, which is available since c++11.
(See online live)
template <typename T, typename X>
constexpr auto Max_Number(T valueA, X valueB)-> decltype(valueA > valueB ? valueA : valueB)
return valueA > valueB ? valueA : valueB;
See some advantages of using trailing return type here: Advantage of using trailing return type in C++11 functions
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',
autoActivateHeartbeat: false,
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/4.0/"u003ecc by-sa 4.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
);
);
Kiran ND is a new contributor. Be nice, and check out our Code of Conduct.
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%2f57951456%2fcan-we-have-a-c-function-with-multiple-return-types-c11-and-above%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
The return type must be determined at compile-time. You might use std::common_type
(since C++11):
For arithmetic types not subject to promotion, the common type may be viewed as the type of the (possibly mixed-mode) arithmetic expression such as
T0() + T1() + ... + Tn()
.
template < typename T, typename X>
std::common_type_t<T, X> Max_Number ( T valueA, X valueB )
...
Or use std::conditional
(since C++11) to declare the return type as the big one (whose sizeof
is greater).
template < typename T, typename X>
std::conditional_t<sizeof(T) >= sizeof(X), T, X> Max_Number ( T valueA, X valueB )
...
add a comment
|
The return type must be determined at compile-time. You might use std::common_type
(since C++11):
For arithmetic types not subject to promotion, the common type may be viewed as the type of the (possibly mixed-mode) arithmetic expression such as
T0() + T1() + ... + Tn()
.
template < typename T, typename X>
std::common_type_t<T, X> Max_Number ( T valueA, X valueB )
...
Or use std::conditional
(since C++11) to declare the return type as the big one (whose sizeof
is greater).
template < typename T, typename X>
std::conditional_t<sizeof(T) >= sizeof(X), T, X> Max_Number ( T valueA, X valueB )
...
add a comment
|
The return type must be determined at compile-time. You might use std::common_type
(since C++11):
For arithmetic types not subject to promotion, the common type may be viewed as the type of the (possibly mixed-mode) arithmetic expression such as
T0() + T1() + ... + Tn()
.
template < typename T, typename X>
std::common_type_t<T, X> Max_Number ( T valueA, X valueB )
...
Or use std::conditional
(since C++11) to declare the return type as the big one (whose sizeof
is greater).
template < typename T, typename X>
std::conditional_t<sizeof(T) >= sizeof(X), T, X> Max_Number ( T valueA, X valueB )
...
The return type must be determined at compile-time. You might use std::common_type
(since C++11):
For arithmetic types not subject to promotion, the common type may be viewed as the type of the (possibly mixed-mode) arithmetic expression such as
T0() + T1() + ... + Tn()
.
template < typename T, typename X>
std::common_type_t<T, X> Max_Number ( T valueA, X valueB )
...
Or use std::conditional
(since C++11) to declare the return type as the big one (whose sizeof
is greater).
template < typename T, typename X>
std::conditional_t<sizeof(T) >= sizeof(X), T, X> Max_Number ( T valueA, X valueB )
...
edited 12 hours ago
answered 12 hours ago
songyuanyaosongyuanyao
106k11 gold badges205 silver badges275 bronze badges
106k11 gold badges205 silver badges275 bronze badges
add a comment
|
add a comment
|
Here is an example solution with std:: variant
template < typename T, typename X>
std::variant<T, X> Max_Number ( T valueA, X valueB )
std::variant<T, X> res;
if ( valueA > valueB )
res = valueA;
else
res = valueB;
return res;
add a comment
|
Here is an example solution with std:: variant
template < typename T, typename X>
std::variant<T, X> Max_Number ( T valueA, X valueB )
std::variant<T, X> res;
if ( valueA > valueB )
res = valueA;
else
res = valueB;
return res;
add a comment
|
Here is an example solution with std:: variant
template < typename T, typename X>
std::variant<T, X> Max_Number ( T valueA, X valueB )
std::variant<T, X> res;
if ( valueA > valueB )
res = valueA;
else
res = valueB;
return res;
Here is an example solution with std:: variant
template < typename T, typename X>
std::variant<T, X> Max_Number ( T valueA, X valueB )
std::variant<T, X> res;
if ( valueA > valueB )
res = valueA;
else
res = valueB;
return res;
edited 12 hours ago
answered 12 hours ago
OblivionOblivion
2,5821 gold badge4 silver badges21 bronze badges
2,5821 gold badge4 silver badges21 bronze badges
add a comment
|
add a comment
|
The Trailing return with a conditional operator is another way to go, which is available since c++11.
(See online live)
template <typename T, typename X>
constexpr auto Max_Number(T valueA, X valueB)-> decltype(valueA > valueB ? valueA : valueB)
return valueA > valueB ? valueA : valueB;
See some advantages of using trailing return type here: Advantage of using trailing return type in C++11 functions
add a comment
|
The Trailing return with a conditional operator is another way to go, which is available since c++11.
(See online live)
template <typename T, typename X>
constexpr auto Max_Number(T valueA, X valueB)-> decltype(valueA > valueB ? valueA : valueB)
return valueA > valueB ? valueA : valueB;
See some advantages of using trailing return type here: Advantage of using trailing return type in C++11 functions
add a comment
|
The Trailing return with a conditional operator is another way to go, which is available since c++11.
(See online live)
template <typename T, typename X>
constexpr auto Max_Number(T valueA, X valueB)-> decltype(valueA > valueB ? valueA : valueB)
return valueA > valueB ? valueA : valueB;
See some advantages of using trailing return type here: Advantage of using trailing return type in C++11 functions
The Trailing return with a conditional operator is another way to go, which is available since c++11.
(See online live)
template <typename T, typename X>
constexpr auto Max_Number(T valueA, X valueB)-> decltype(valueA > valueB ? valueA : valueB)
return valueA > valueB ? valueA : valueB;
See some advantages of using trailing return type here: Advantage of using trailing return type in C++11 functions
answered 39 mins ago
JeJoJeJo
9,8493 gold badges15 silver badges45 bronze badges
9,8493 gold badges15 silver badges45 bronze badges
add a comment
|
add a comment
|
Kiran ND is a new contributor. Be nice, and check out our Code of Conduct.
Kiran ND is a new contributor. Be nice, and check out our Code of Conduct.
Kiran ND is a new contributor. Be nice, and check out our Code of Conduct.
Kiran ND is a new contributor. Be nice, and check out our Code of Conduct.
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.
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%2f57951456%2fcan-we-have-a-c-function-with-multiple-return-types-c11-and-above%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
3
take a look at
std::variant
That's probably the closest since C++ is strongly typed– doug
12 hours ago
There might be some template wizards that could suggest something, but I'm fairly certain that the return type must be fixed
– selbie
12 hours ago
4
Have you tried to use plain
std::max
? Remember that integer types can safely be implicitly converted to a larger type.– Some programmer dude
12 hours ago
@Someprogrammerdude Yep, that should work given his example's statement that the largest arg magnitude is also the largest datatype applies in all other cases.
– doug
12 hours ago
Are the arguments always unsigned? It's not clear because of that "...". If they are, I would use simply
uintmax_t Max_Number(uintmax_t a, uintmax_t b) ...
.– Daniel Langr
11 hours ago