1,105,352 Community Members

What does unary + do?

Member Avatar
Labdabeta
Master Poster
761 posts since Feb 2011
Reputation Points: 161 [?]
Q&As Helped to Solve: 42 [?]
Skill Endorsements: 6 [?]
 
0
 

I am really confused as to the purpose of the unary + operator. What in the world does it do? I have tested it and it doesn't return the absolute value. Does it just return the operand? And if so what purpose is it?! (I mainly care about integer types)

Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
0
 

You will see the answer on this page: http://en.wikipedia.org/wiki/Unary_operation

Member Avatar
PrimePackster
Posting Whiz in Training
252 posts since Nov 2011
Reputation Points: 10 [?]
Q&As Helped to Solve: 17 [?]
Skill Endorsements: 3 [?]
 
0
 

A single +, it just tells the compiler that it is a positive value.

+3 means 3 is positive, thats all
Well about usage of +, there is short hand methods in C++, Like this; d+=5 Here it means d=d+5.
But since it uses 2 operands, its no longer unary operator (Just mentioned, so that you don't get confused with all these usages).

Member Avatar
firstPerson
Industrious Poster
4,052 posts since Dec 2008
Reputation Points: 761 [?]
Q&As Helped to Solve: 634 [?]
Skill Endorsements: 24 [?]
 
0
 

I kind of like the idea of unary plus being an absolute value sign. Right now, the unary plus is nothing more than extra information to the user. Maybe to hint to the user that it should always be a plus or something. It has no effect.

Member Avatar
PrimePackster
Posting Whiz in Training
252 posts since Nov 2011
Reputation Points: 10 [?]
Q&As Helped to Solve: 17 [?]
Skill Endorsements: 3 [?]
 
0
 

I kind of like the idea of unary plus being an absolute value sign.

Means? Absolute value refers to be value having no dependence on its sign right? So what is an absolute value sign?

Member Avatar
vijayan121
Posting Virtuoso
1,769 posts since Dec 2006
Reputation Points: 1,097 [?]
Q&As Helped to Solve: 329 [?]
Skill Endorsements: 16 [?]
 
0
 

> It has no effect

For standard types, a unary + always yields an r-value.

unsigned short s = 8 ;
unsigned short& r = s ; // s is an l-value
unsigned short&& rvr = +s ; // +s yiels an r-value

For standard types, a A unary + performs a promotion where applicable.

unsigned short s = 8 ;
auto x = s ; // type of x is unsigned short
auto y = +s ; // type of y is signed int
    //(unsigned int if int cannot represent the full range of unsigned short)

char c = 'A' ;
std::cout << c << ' ' << +c << '\n'

For user-defined types, the overloaded unary + operator could do anything one pleases.

#include <iostream>
#include <string>
#include <boost/xpressive/xpressive.hpp>

using namespace boost::xpressive ;

void foo( const std::string& str, const sregex& re )
{
    sregex_iterator iter( str.begin(), str.end(), re ), end ;
    for( ; iter != end ; ++iter ) std::cout << '[' << (*iter)[0] << "] " ;
    std::cout << '\n' ;
}

int main()
{
    std::string str = "abcd1234efgh789ij" ;

    foo( str, _d ) ; // prints 7 matches:  [1] [2] [3] [4] [7] [8] [9]

    foo( str, +_d ) ; // prints 2 matches: [1234] [789]
}
Member Avatar
firstPerson
Industrious Poster
4,052 posts since Dec 2008
Reputation Points: 761 [?]
Q&As Helped to Solve: 634 [?]
Skill Endorsements: 24 [?]
 
0
 

Means? Absolute value refers to be value having no dependence on its sign right? So what is an absolute value sign?

As in the mathematical sense of absolute value. In mathematics one would use the Bar symbol as in , |x|, using the unary + symbol, as in +x, kind of amused me a little.

Question Answered as of 2 Years Ago by PrimePackster, firstPerson, thines01 and 1 other
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: