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;








6















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.










share|improve this question









New contributor



Kiran ND is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 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


















6















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.










share|improve this question









New contributor



Kiran ND is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 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














6












6








6








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.










share|improve this question









New contributor



Kiran ND is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question









New contributor



Kiran ND is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share|improve this question









New contributor



Kiran ND is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this question




share|improve this question








edited 12 hours ago









Oblivion

2,5821 gold badge4 silver badges21 bronze badges




2,5821 gold badge4 silver badges21 bronze badges






New contributor



Kiran ND is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked 12 hours ago









Kiran NDKiran ND

311 bronze badge




311 bronze badge




New contributor



Kiran ND is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




Kiran ND is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • 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













  • 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








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













3 Answers
3






active

oldest

votes


















10
















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 )
...






share|improve this answer


































    6
















    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;






    share|improve this answer


































      1
















      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






      share|improve this answer



























        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.









        draft saved

        draft discarded
















        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









        10
















        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 )
        ...






        share|improve this answer































          10
















          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 )
          ...






          share|improve this answer





























            10














            10










            10









            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 )
            ...






            share|improve this answer















            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 )
            ...







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 12 hours ago

























            answered 12 hours ago









            songyuanyaosongyuanyao

            106k11 gold badges205 silver badges275 bronze badges




            106k11 gold badges205 silver badges275 bronze badges


























                6
















                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;






                share|improve this answer































                  6
















                  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;






                  share|improve this answer





























                    6














                    6










                    6









                    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;






                    share|improve this answer















                    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;







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    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
























                        1
















                        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






                        share|improve this answer





























                          1
















                          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






                          share|improve this answer



























                            1














                            1










                            1









                            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






                            share|improve this answer













                            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







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 39 mins ago









                            JeJoJeJo

                            9,8493 gold badges15 silver badges45 bronze badges




                            9,8493 gold badges15 silver badges45 bronze badges
























                                Kiran ND is a new contributor. Be nice, and check out our Code of Conduct.









                                draft saved

                                draft discarded

















                                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.




                                draft saved


                                draft discarded














                                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





















































                                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







                                Popular posts from this blog

                                ParseJSON using SSJSUsing AMPscript with SSJS ActivitiesHow to resubscribe a user in Marketing cloud using SSJS?Pulling Subscriber Status from Lists using SSJSRetrieving Emails using SSJSProblem in updating DE using SSJSUsing SSJS to send single email in Marketing CloudError adding EmailSendDefinition using SSJS

                                Кампала Садржај Географија Географија Историја Становништво Привреда Партнерски градови Референце Спољашње везе Мени за навигацију0°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.340°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.34МедијиПодациЗванични веб-сајту

                                19. јануар Садржај Догађаји Рођења Смрти Празници и дани сећања Види још Референце Мени за навигацијуу