What is the purpose of a unary “+” before a call to std::numeric_limits members?

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











up vote
103
down vote

favorite
8












I saw this example in cppreference's documentation for std::numeric_limits



#include <limits>
#include <iostream>

int main()

std::cout << "typetlowest()tmin()ttmax()nn";

std::cout << "uchart"
<< +std::numeric_limits<unsigned char>::lowest() << 't' << 't'
<< +std::numeric_limits<unsigned char>::min() << 't' << 't'
<< +std::numeric_limits<unsigned char>::max() << 'n';
std::cout << "intt"
<< std::numeric_limits<int>::lowest() << 't'
<< std::numeric_limits<int>::min() << 't'
<< std::numeric_limits<int>::max() << 'n';
std::cout << "floatt"
<< std::numeric_limits<float>::lowest() << 't'
<< std::numeric_limits<float>::min() << 't'
<< std::numeric_limits<float>::max() << 'n';
std::cout << "doublet"
<< std::numeric_limits<double>::lowest() << 't'
<< std::numeric_limits<double>::min() << 't'
<< std::numeric_limits<double>::max() << 'n';



I don't understand the "+" operator in



<< +std::numeric_limits<unsigned char>::lowest()


I have tested it, replaced it with "-", and that also worked.
What is the use of such a "+" operator?







share|improve this question


















  • 7




    Good question about a non-obvious thing!
    – StoryTeller
    Sep 3 at 8:55






  • 3




    Try it. What do you get if you leave out the +?
    – Pete Becker
    Sep 3 at 12:36






  • 4




    This question would not need to be asker if the writer of the code cares to explain what it means or use an explicit cast instead...
    – user202729
    Sep 3 at 13:57










  • if you replace with - then the outputs won't be the correct values for the limits
    – phuclv
    Sep 3 at 18:28







  • 1




    For the record, if you want to Google this kind of thing in yourself, this is called the "unary plus" operator -- it's unary because it takes a single value (in this case, the thing right after it), and "plus" is the Google-friendly way to say +. In this case, your query would probably be "c++ unary plus". It's... not exactly intuitive, and you still have to learn to read the documentation that you'll find, but IMO it's a useful skill to cultivate.
    – Nic Hartley
    Sep 4 at 19:46















up vote
103
down vote

favorite
8












I saw this example in cppreference's documentation for std::numeric_limits



#include <limits>
#include <iostream>

int main()

std::cout << "typetlowest()tmin()ttmax()nn";

std::cout << "uchart"
<< +std::numeric_limits<unsigned char>::lowest() << 't' << 't'
<< +std::numeric_limits<unsigned char>::min() << 't' << 't'
<< +std::numeric_limits<unsigned char>::max() << 'n';
std::cout << "intt"
<< std::numeric_limits<int>::lowest() << 't'
<< std::numeric_limits<int>::min() << 't'
<< std::numeric_limits<int>::max() << 'n';
std::cout << "floatt"
<< std::numeric_limits<float>::lowest() << 't'
<< std::numeric_limits<float>::min() << 't'
<< std::numeric_limits<float>::max() << 'n';
std::cout << "doublet"
<< std::numeric_limits<double>::lowest() << 't'
<< std::numeric_limits<double>::min() << 't'
<< std::numeric_limits<double>::max() << 'n';



I don't understand the "+" operator in



<< +std::numeric_limits<unsigned char>::lowest()


I have tested it, replaced it with "-", and that also worked.
What is the use of such a "+" operator?







share|improve this question


















  • 7




    Good question about a non-obvious thing!
    – StoryTeller
    Sep 3 at 8:55






  • 3




    Try it. What do you get if you leave out the +?
    – Pete Becker
    Sep 3 at 12:36






  • 4




    This question would not need to be asker if the writer of the code cares to explain what it means or use an explicit cast instead...
    – user202729
    Sep 3 at 13:57










  • if you replace with - then the outputs won't be the correct values for the limits
    – phuclv
    Sep 3 at 18:28







  • 1




    For the record, if you want to Google this kind of thing in yourself, this is called the "unary plus" operator -- it's unary because it takes a single value (in this case, the thing right after it), and "plus" is the Google-friendly way to say +. In this case, your query would probably be "c++ unary plus". It's... not exactly intuitive, and you still have to learn to read the documentation that you'll find, but IMO it's a useful skill to cultivate.
    – Nic Hartley
    Sep 4 at 19:46













up vote
103
down vote

favorite
8









up vote
103
down vote

favorite
8






8





I saw this example in cppreference's documentation for std::numeric_limits



#include <limits>
#include <iostream>

int main()

std::cout << "typetlowest()tmin()ttmax()nn";

std::cout << "uchart"
<< +std::numeric_limits<unsigned char>::lowest() << 't' << 't'
<< +std::numeric_limits<unsigned char>::min() << 't' << 't'
<< +std::numeric_limits<unsigned char>::max() << 'n';
std::cout << "intt"
<< std::numeric_limits<int>::lowest() << 't'
<< std::numeric_limits<int>::min() << 't'
<< std::numeric_limits<int>::max() << 'n';
std::cout << "floatt"
<< std::numeric_limits<float>::lowest() << 't'
<< std::numeric_limits<float>::min() << 't'
<< std::numeric_limits<float>::max() << 'n';
std::cout << "doublet"
<< std::numeric_limits<double>::lowest() << 't'
<< std::numeric_limits<double>::min() << 't'
<< std::numeric_limits<double>::max() << 'n';



I don't understand the "+" operator in



<< +std::numeric_limits<unsigned char>::lowest()


I have tested it, replaced it with "-", and that also worked.
What is the use of such a "+" operator?







share|improve this question














I saw this example in cppreference's documentation for std::numeric_limits



#include <limits>
#include <iostream>

int main()

std::cout << "typetlowest()tmin()ttmax()nn";

std::cout << "uchart"
<< +std::numeric_limits<unsigned char>::lowest() << 't' << 't'
<< +std::numeric_limits<unsigned char>::min() << 't' << 't'
<< +std::numeric_limits<unsigned char>::max() << 'n';
std::cout << "intt"
<< std::numeric_limits<int>::lowest() << 't'
<< std::numeric_limits<int>::min() << 't'
<< std::numeric_limits<int>::max() << 'n';
std::cout << "floatt"
<< std::numeric_limits<float>::lowest() << 't'
<< std::numeric_limits<float>::min() << 't'
<< std::numeric_limits<float>::max() << 'n';
std::cout << "doublet"
<< std::numeric_limits<double>::lowest() << 't'
<< std::numeric_limits<double>::min() << 't'
<< std::numeric_limits<double>::max() << 'n';



I don't understand the "+" operator in



<< +std::numeric_limits<unsigned char>::lowest()


I have tested it, replaced it with "-", and that also worked.
What is the use of such a "+" operator?









share|improve this question













share|improve this question




share|improve this question








edited Sep 3 at 18:28









phuclv

13.2k846183




13.2k846183










asked Sep 3 at 8:43









马化腾

6881212




6881212







  • 7




    Good question about a non-obvious thing!
    – StoryTeller
    Sep 3 at 8:55






  • 3




    Try it. What do you get if you leave out the +?
    – Pete Becker
    Sep 3 at 12:36






  • 4




    This question would not need to be asker if the writer of the code cares to explain what it means or use an explicit cast instead...
    – user202729
    Sep 3 at 13:57










  • if you replace with - then the outputs won't be the correct values for the limits
    – phuclv
    Sep 3 at 18:28







  • 1




    For the record, if you want to Google this kind of thing in yourself, this is called the "unary plus" operator -- it's unary because it takes a single value (in this case, the thing right after it), and "plus" is the Google-friendly way to say +. In this case, your query would probably be "c++ unary plus". It's... not exactly intuitive, and you still have to learn to read the documentation that you'll find, but IMO it's a useful skill to cultivate.
    – Nic Hartley
    Sep 4 at 19:46













  • 7




    Good question about a non-obvious thing!
    – StoryTeller
    Sep 3 at 8:55






  • 3




    Try it. What do you get if you leave out the +?
    – Pete Becker
    Sep 3 at 12:36






  • 4




    This question would not need to be asker if the writer of the code cares to explain what it means or use an explicit cast instead...
    – user202729
    Sep 3 at 13:57










  • if you replace with - then the outputs won't be the correct values for the limits
    – phuclv
    Sep 3 at 18:28







  • 1




    For the record, if you want to Google this kind of thing in yourself, this is called the "unary plus" operator -- it's unary because it takes a single value (in this case, the thing right after it), and "plus" is the Google-friendly way to say +. In this case, your query would probably be "c++ unary plus". It's... not exactly intuitive, and you still have to learn to read the documentation that you'll find, but IMO it's a useful skill to cultivate.
    – Nic Hartley
    Sep 4 at 19:46








7




7




Good question about a non-obvious thing!
– StoryTeller
Sep 3 at 8:55




Good question about a non-obvious thing!
– StoryTeller
Sep 3 at 8:55




3




3




Try it. What do you get if you leave out the +?
– Pete Becker
Sep 3 at 12:36




Try it. What do you get if you leave out the +?
– Pete Becker
Sep 3 at 12:36




4




4




This question would not need to be asker if the writer of the code cares to explain what it means or use an explicit cast instead...
– user202729
Sep 3 at 13:57




This question would not need to be asker if the writer of the code cares to explain what it means or use an explicit cast instead...
– user202729
Sep 3 at 13:57












if you replace with - then the outputs won't be the correct values for the limits
– phuclv
Sep 3 at 18:28





if you replace with - then the outputs won't be the correct values for the limits
– phuclv
Sep 3 at 18:28





1




1




For the record, if you want to Google this kind of thing in yourself, this is called the "unary plus" operator -- it's unary because it takes a single value (in this case, the thing right after it), and "plus" is the Google-friendly way to say +. In this case, your query would probably be "c++ unary plus". It's... not exactly intuitive, and you still have to learn to read the documentation that you'll find, but IMO it's a useful skill to cultivate.
– Nic Hartley
Sep 4 at 19:46





For the record, if you want to Google this kind of thing in yourself, this is called the "unary plus" operator -- it's unary because it takes a single value (in this case, the thing right after it), and "plus" is the Google-friendly way to say +. In this case, your query would probably be "c++ unary plus". It's... not exactly intuitive, and you still have to learn to read the documentation that you'll find, but IMO it's a useful skill to cultivate.
– Nic Hartley
Sep 4 at 19:46













4 Answers
4






active

oldest

votes

















up vote
123
down vote



accepted










The output operator << when being passed a char (signed or unsigned) will write it as a character.



Those function will return values of type unsigned char. And as noted above that will print the characters those values represent in the current encoding, not their integer values.



The + operator converts the unsigned char returned by those functions to an int through integer promotion. Which means the integer values will be printed instead.



An expression like +std::numeric_limits<unsigned char>::lowest() is essentially equal to static_cast<int>(std::numeric_limits<unsigned char>::lowest()).






share|improve this answer





























    up vote
    32
    down vote













    + is there to turn the unsigned char into an int. The + operator is value preserving, but it has the effect of inducing integral promotion on its operand. It's to make sure you see a numerical value instead of some (semi-)random character that operator << would print when given a character type.






    share|improve this answer



























      up vote
      14
      down vote













      Just to add a reference to the answers already given. From the CPP standard working draft N4713:




      8.5.2.1 Unary operators

      ...




      1. The operand of the unary + operator shall have arithmetic, unscoped enumeration, or pointer type and the result is the value of the argument. Integral promotion is performed on integral or enumeration operands. The type of the result is the type of the promoted operand.




      And char, short, int, and long are integral types.






      share|improve this answer





























        up vote
        8
        down vote













        Without + the result will be different. The following snippet outputs a 97 instead of a a



        char ch = 'a';
        std::cout << ch << ' ' << +ch << 'n';


        The reason is because different overloads prints different types of data.
        There's no basic_ostream& operator<<( char value ); overload for std::basic_ostream and it's explained at the end of the page




        Character and character string arguments (e.g., of type char or const char*) are handled by the non-member overloads of operator<<. Attempting to output a character using the member function call syntax (e.g., std::cout.operator<<('c');) will call one of overloads (2-4) and output the numerical value. Attempting to output a character string using the member function call syntax will call overload (7) and print the pointer value instead.




        The non-member overload that will be called when you pass a char variable is



        template< class CharT, class Traits> basic_ostream<CharT,Traits>& operator<<(
        basic_ostream<CharT,Traits>& os, char ch );


        which prints out the character at the codepoint ch



        So basically if you pass char, signed char or unsigned char directly to the stream it'll print the character out. If you try removing the + on the above lines you'll see that it prints some "strange" or non-visible characters which is not what one would expect



        If you want their numerical values instead you must call the overload for short, int, long or long long. The easiest way to do this is promoting from char to int with unary plus +. That's one of the rare useful applications of the unary plus operator. An explicit cast to int will also work



        There are many people who faced that problem on SO like



        • cout not printing unsigned char

        • std::cout deal with uint8_t as a character

        • uint8_t can't be printed with cout





        share|improve this answer


















        • 1




          Did you mean unary plus, not minus?
          – Ruslan
          Sep 3 at 16:39










        • @Ruslan yes, that's a typo. Thanks
          – phuclv
          Sep 3 at 18:18











        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%2f52146115%2fwhat-is-the-purpose-of-a-unary-before-a-call-to-stdnumeric-limitsunsigned%23new-answer', 'question_page');

        );

        Post as a guest






























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        123
        down vote



        accepted










        The output operator << when being passed a char (signed or unsigned) will write it as a character.



        Those function will return values of type unsigned char. And as noted above that will print the characters those values represent in the current encoding, not their integer values.



        The + operator converts the unsigned char returned by those functions to an int through integer promotion. Which means the integer values will be printed instead.



        An expression like +std::numeric_limits<unsigned char>::lowest() is essentially equal to static_cast<int>(std::numeric_limits<unsigned char>::lowest()).






        share|improve this answer


























          up vote
          123
          down vote



          accepted










          The output operator << when being passed a char (signed or unsigned) will write it as a character.



          Those function will return values of type unsigned char. And as noted above that will print the characters those values represent in the current encoding, not their integer values.



          The + operator converts the unsigned char returned by those functions to an int through integer promotion. Which means the integer values will be printed instead.



          An expression like +std::numeric_limits<unsigned char>::lowest() is essentially equal to static_cast<int>(std::numeric_limits<unsigned char>::lowest()).






          share|improve this answer
























            up vote
            123
            down vote



            accepted







            up vote
            123
            down vote



            accepted






            The output operator << when being passed a char (signed or unsigned) will write it as a character.



            Those function will return values of type unsigned char. And as noted above that will print the characters those values represent in the current encoding, not their integer values.



            The + operator converts the unsigned char returned by those functions to an int through integer promotion. Which means the integer values will be printed instead.



            An expression like +std::numeric_limits<unsigned char>::lowest() is essentially equal to static_cast<int>(std::numeric_limits<unsigned char>::lowest()).






            share|improve this answer














            The output operator << when being passed a char (signed or unsigned) will write it as a character.



            Those function will return values of type unsigned char. And as noted above that will print the characters those values represent in the current encoding, not their integer values.



            The + operator converts the unsigned char returned by those functions to an int through integer promotion. Which means the integer values will be printed instead.



            An expression like +std::numeric_limits<unsigned char>::lowest() is essentially equal to static_cast<int>(std::numeric_limits<unsigned char>::lowest()).







            share|improve this answer














            share|improve this answer



            share|improve this answer








            answered Sep 3 at 8:46


























            community wiki





            Some programmer dude























                up vote
                32
                down vote













                + is there to turn the unsigned char into an int. The + operator is value preserving, but it has the effect of inducing integral promotion on its operand. It's to make sure you see a numerical value instead of some (semi-)random character that operator << would print when given a character type.






                share|improve this answer
























                  up vote
                  32
                  down vote













                  + is there to turn the unsigned char into an int. The + operator is value preserving, but it has the effect of inducing integral promotion on its operand. It's to make sure you see a numerical value instead of some (semi-)random character that operator << would print when given a character type.






                  share|improve this answer






















                    up vote
                    32
                    down vote










                    up vote
                    32
                    down vote









                    + is there to turn the unsigned char into an int. The + operator is value preserving, but it has the effect of inducing integral promotion on its operand. It's to make sure you see a numerical value instead of some (semi-)random character that operator << would print when given a character type.






                    share|improve this answer












                    + is there to turn the unsigned char into an int. The + operator is value preserving, but it has the effect of inducing integral promotion on its operand. It's to make sure you see a numerical value instead of some (semi-)random character that operator << would print when given a character type.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Sep 3 at 8:46









                    StoryTeller

                    82.4k12166228




                    82.4k12166228




















                        up vote
                        14
                        down vote













                        Just to add a reference to the answers already given. From the CPP standard working draft N4713:




                        8.5.2.1 Unary operators

                        ...




                        1. The operand of the unary + operator shall have arithmetic, unscoped enumeration, or pointer type and the result is the value of the argument. Integral promotion is performed on integral or enumeration operands. The type of the result is the type of the promoted operand.




                        And char, short, int, and long are integral types.






                        share|improve this answer


























                          up vote
                          14
                          down vote













                          Just to add a reference to the answers already given. From the CPP standard working draft N4713:




                          8.5.2.1 Unary operators

                          ...




                          1. The operand of the unary + operator shall have arithmetic, unscoped enumeration, or pointer type and the result is the value of the argument. Integral promotion is performed on integral or enumeration operands. The type of the result is the type of the promoted operand.




                          And char, short, int, and long are integral types.






                          share|improve this answer
























                            up vote
                            14
                            down vote










                            up vote
                            14
                            down vote









                            Just to add a reference to the answers already given. From the CPP standard working draft N4713:




                            8.5.2.1 Unary operators

                            ...




                            1. The operand of the unary + operator shall have arithmetic, unscoped enumeration, or pointer type and the result is the value of the argument. Integral promotion is performed on integral or enumeration operands. The type of the result is the type of the promoted operand.




                            And char, short, int, and long are integral types.






                            share|improve this answer














                            Just to add a reference to the answers already given. From the CPP standard working draft N4713:




                            8.5.2.1 Unary operators

                            ...




                            1. The operand of the unary + operator shall have arithmetic, unscoped enumeration, or pointer type and the result is the value of the argument. Integral promotion is performed on integral or enumeration operands. The type of the result is the type of the promoted operand.




                            And char, short, int, and long are integral types.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Sep 4 at 19:23









                            Casey

                            33k66399




                            33k66399










                            answered Sep 3 at 9:31









                            P.W

                            2,402120




                            2,402120




















                                up vote
                                8
                                down vote













                                Without + the result will be different. The following snippet outputs a 97 instead of a a



                                char ch = 'a';
                                std::cout << ch << ' ' << +ch << 'n';


                                The reason is because different overloads prints different types of data.
                                There's no basic_ostream& operator<<( char value ); overload for std::basic_ostream and it's explained at the end of the page




                                Character and character string arguments (e.g., of type char or const char*) are handled by the non-member overloads of operator<<. Attempting to output a character using the member function call syntax (e.g., std::cout.operator<<('c');) will call one of overloads (2-4) and output the numerical value. Attempting to output a character string using the member function call syntax will call overload (7) and print the pointer value instead.




                                The non-member overload that will be called when you pass a char variable is



                                template< class CharT, class Traits> basic_ostream<CharT,Traits>& operator<<(
                                basic_ostream<CharT,Traits>& os, char ch );


                                which prints out the character at the codepoint ch



                                So basically if you pass char, signed char or unsigned char directly to the stream it'll print the character out. If you try removing the + on the above lines you'll see that it prints some "strange" or non-visible characters which is not what one would expect



                                If you want their numerical values instead you must call the overload for short, int, long or long long. The easiest way to do this is promoting from char to int with unary plus +. That's one of the rare useful applications of the unary plus operator. An explicit cast to int will also work



                                There are many people who faced that problem on SO like



                                • cout not printing unsigned char

                                • std::cout deal with uint8_t as a character

                                • uint8_t can't be printed with cout





                                share|improve this answer


















                                • 1




                                  Did you mean unary plus, not minus?
                                  – Ruslan
                                  Sep 3 at 16:39










                                • @Ruslan yes, that's a typo. Thanks
                                  – phuclv
                                  Sep 3 at 18:18















                                up vote
                                8
                                down vote













                                Without + the result will be different. The following snippet outputs a 97 instead of a a



                                char ch = 'a';
                                std::cout << ch << ' ' << +ch << 'n';


                                The reason is because different overloads prints different types of data.
                                There's no basic_ostream& operator<<( char value ); overload for std::basic_ostream and it's explained at the end of the page




                                Character and character string arguments (e.g., of type char or const char*) are handled by the non-member overloads of operator<<. Attempting to output a character using the member function call syntax (e.g., std::cout.operator<<('c');) will call one of overloads (2-4) and output the numerical value. Attempting to output a character string using the member function call syntax will call overload (7) and print the pointer value instead.




                                The non-member overload that will be called when you pass a char variable is



                                template< class CharT, class Traits> basic_ostream<CharT,Traits>& operator<<(
                                basic_ostream<CharT,Traits>& os, char ch );


                                which prints out the character at the codepoint ch



                                So basically if you pass char, signed char or unsigned char directly to the stream it'll print the character out. If you try removing the + on the above lines you'll see that it prints some "strange" or non-visible characters which is not what one would expect



                                If you want their numerical values instead you must call the overload for short, int, long or long long. The easiest way to do this is promoting from char to int with unary plus +. That's one of the rare useful applications of the unary plus operator. An explicit cast to int will also work



                                There are many people who faced that problem on SO like



                                • cout not printing unsigned char

                                • std::cout deal with uint8_t as a character

                                • uint8_t can't be printed with cout





                                share|improve this answer


















                                • 1




                                  Did you mean unary plus, not minus?
                                  – Ruslan
                                  Sep 3 at 16:39










                                • @Ruslan yes, that's a typo. Thanks
                                  – phuclv
                                  Sep 3 at 18:18













                                up vote
                                8
                                down vote










                                up vote
                                8
                                down vote









                                Without + the result will be different. The following snippet outputs a 97 instead of a a



                                char ch = 'a';
                                std::cout << ch << ' ' << +ch << 'n';


                                The reason is because different overloads prints different types of data.
                                There's no basic_ostream& operator<<( char value ); overload for std::basic_ostream and it's explained at the end of the page




                                Character and character string arguments (e.g., of type char or const char*) are handled by the non-member overloads of operator<<. Attempting to output a character using the member function call syntax (e.g., std::cout.operator<<('c');) will call one of overloads (2-4) and output the numerical value. Attempting to output a character string using the member function call syntax will call overload (7) and print the pointer value instead.




                                The non-member overload that will be called when you pass a char variable is



                                template< class CharT, class Traits> basic_ostream<CharT,Traits>& operator<<(
                                basic_ostream<CharT,Traits>& os, char ch );


                                which prints out the character at the codepoint ch



                                So basically if you pass char, signed char or unsigned char directly to the stream it'll print the character out. If you try removing the + on the above lines you'll see that it prints some "strange" or non-visible characters which is not what one would expect



                                If you want their numerical values instead you must call the overload for short, int, long or long long. The easiest way to do this is promoting from char to int with unary plus +. That's one of the rare useful applications of the unary plus operator. An explicit cast to int will also work



                                There are many people who faced that problem on SO like



                                • cout not printing unsigned char

                                • std::cout deal with uint8_t as a character

                                • uint8_t can't be printed with cout





                                share|improve this answer














                                Without + the result will be different. The following snippet outputs a 97 instead of a a



                                char ch = 'a';
                                std::cout << ch << ' ' << +ch << 'n';


                                The reason is because different overloads prints different types of data.
                                There's no basic_ostream& operator<<( char value ); overload for std::basic_ostream and it's explained at the end of the page




                                Character and character string arguments (e.g., of type char or const char*) are handled by the non-member overloads of operator<<. Attempting to output a character using the member function call syntax (e.g., std::cout.operator<<('c');) will call one of overloads (2-4) and output the numerical value. Attempting to output a character string using the member function call syntax will call overload (7) and print the pointer value instead.




                                The non-member overload that will be called when you pass a char variable is



                                template< class CharT, class Traits> basic_ostream<CharT,Traits>& operator<<(
                                basic_ostream<CharT,Traits>& os, char ch );


                                which prints out the character at the codepoint ch



                                So basically if you pass char, signed char or unsigned char directly to the stream it'll print the character out. If you try removing the + on the above lines you'll see that it prints some "strange" or non-visible characters which is not what one would expect



                                If you want their numerical values instead you must call the overload for short, int, long or long long. The easiest way to do this is promoting from char to int with unary plus +. That's one of the rare useful applications of the unary plus operator. An explicit cast to int will also work



                                There are many people who faced that problem on SO like



                                • cout not printing unsigned char

                                • std::cout deal with uint8_t as a character

                                • uint8_t can't be printed with cout






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Sep 5 at 5:36

























                                answered Sep 3 at 10:25









                                phuclv

                                13.2k846183




                                13.2k846183







                                • 1




                                  Did you mean unary plus, not minus?
                                  – Ruslan
                                  Sep 3 at 16:39










                                • @Ruslan yes, that's a typo. Thanks
                                  – phuclv
                                  Sep 3 at 18:18













                                • 1




                                  Did you mean unary plus, not minus?
                                  – Ruslan
                                  Sep 3 at 16:39










                                • @Ruslan yes, that's a typo. Thanks
                                  – phuclv
                                  Sep 3 at 18:18








                                1




                                1




                                Did you mean unary plus, not minus?
                                – Ruslan
                                Sep 3 at 16:39




                                Did you mean unary plus, not minus?
                                – Ruslan
                                Sep 3 at 16:39












                                @Ruslan yes, that's a typo. Thanks
                                – phuclv
                                Sep 3 at 18:18





                                @Ruslan yes, that's a typo. Thanks
                                – phuclv
                                Sep 3 at 18:18


















                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52146115%2fwhat-is-the-purpose-of-a-unary-before-a-call-to-stdnumeric-limitsunsigned%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Comments

                                Popular posts from this blog

                                What does second last employer means? [closed]

                                Installing NextGIS Connect into QGIS 3?

                                One-line joke