unary-operator

Does the unary + operator have any practical use?

血红的双手。 提交于 2019-11-28 11:53:08
Was the unary + operator only included for symmetry with the unary - operator, or does it find some practical use in C++ code? Searching here, I came across What is the purpose of the unary '+' operator in C? , but the only useful scenarios there involve preprocessor macros. Those are good to know, but they seem to be some less common situations, and involve macros. Are there any use cases involving more common C++ code? char ch = 'a'; std::cout << ch << '\n'; std::cout << +ch << '\n'; The first insertion writes the character a to cout . The second insertion writes the numeric value of ch to

Multiple increment operators in single statement [duplicate]

微笑、不失礼 提交于 2019-11-28 10:44:15
问题 Possible Duplicate: Undefined Behavior and Sequence Points Pleae explain the behaviour of following statements int b=3; cout<<b++*++b<<endl; How will it be calculated? 回答1: The behavior here is undefined. See this question Relevant standard quote: §5/4.1 Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression. The most common sequence point is the end of a statement. Also worth noting from the standard:

encounter “unary operator expected” in bash script

﹥>﹥吖頭↗ 提交于 2019-11-28 09:12:48
in my bash script, I have a function to return 0 or 1(true or false) for the later main function's condition. function1 () { if [[ "${1}" =~ "^ ...some regexp... $" ]] ; then return 1 else return 0 fi } then in my main function: main () { for arg in ${@} ; do if [ function1 ${arg} ] ; then ... elif [ ... ] ; then ... fi done } however, when i ran this script it always gave me an error msg "[: function1: unary operator expected" can anyone help me please? You are making the common mistake of assuming that [ is part of the if command's syntax. It is not; the syntax of if is simply if command;

What's the point of unary plus operator in Ruby?

主宰稳场 提交于 2019-11-28 01:03:50
Apart from making a nice symmetry with unary minus, why is unary plus operator defined on Numeric class? Is there some practical value in it, except for causing confusion allowing writing things like ++i (which, unlike most non-Rubyists would think, doesn't increment i ). I can think of scenario where defining unary plus on a custom class could be useful (say if you're creating some sexy DSL), so being able to define it is ok, but why is it already defined on Ruby numbers? Perhaps it's just a matter of consistency, both with other programming languages, and to mirror the unary minus. Found

Does it make sense for unary operators to be associative?

≯℡__Kan透↙ 提交于 2019-11-28 00:45:17
The C++ operator precedence table from http://en.cppreference.com/w/cpp/language/operator_precedence (I know it's not normative, but the standard doesn't talk about precedence or associativity) marks unary operators as right/left associative. From a discussion on a different question, I'm left with doubts. Does it make sense for unary operators to be associative? It's just an artefact of the way that the associativity is derived from the grammar. The reason that addition is left-associative is that one of the productions for additive-expression is additive-expression + multiplicative

What does +@ mean as a method in ruby

回眸只為那壹抹淺笑 提交于 2019-11-27 16:34:55
问题 I was reading some code and I saw something along the lines of module M def +@ self end end I was surprised that this was legal syntax, yet when I ran ruby -c on the file (to lint) it said it was valid. -@ was also a legal method name yet when I tried *@ or d@ both of those were illegal. I was wondering what +@ means and why is it legal? 回答1: Ruby contains a few unary operators, including + , - , ! , ~ , & and * . As with other operators you can also redefine these. For ~ and ! you can simply

Why would one use the unary operator on a property in ruby? i.e &:first [duplicate]

人走茶凉 提交于 2019-11-27 15:58:17
Possible Duplicate: Ruby/Ruby on Rails ampersand colon shortcut As a habit I try and read a little of someone elses source code regularly and comment on it in a gist. Right now I'm reading through sinatra's base app and came upon an interesting bit of code (this is part of their Request class) def accept @env['sinatra.accept'] ||= begin entries = @env['HTTP_ACCEPT'].to_s.split(',') entries.map { |e| accept_entry(e) }.sort_by(&:last).map(&:first) end end The part I don't get is what is &:last and &:first doing?!? It appears as madness! Read the answers in the duplicate questions for the meaning

+ operator before expression in javascript: what does it do?

廉价感情. 提交于 2019-11-27 15:32:06
I was perusing the underscore.js library and I found something I haven't come across before: if (obj.length === +obj.length) { ... } What is that + operator doing there? For context, here is a direct link to that part of the file. The unary + operator can be used to convert a value to a number in JavaScript. Underscore appears to be testing that the .length property is a number, otherwise it won't be equal to itself-converted-to-a-number. According to MDN : The unary plus operator precedes its operand and evaluates to its operand but attempts to converts it into a number, if it isn't already.

What is the difference between += and =+?

不打扰是莪最后的温柔 提交于 2019-11-27 14:43:00
What is the difference between += and =+? Specifically, in java, but in general also. i += 4; means i = i + 4; // increase i by 4. While i =+ 4; is equivalent to i = +4; // assign 4 to i. the unary plus is effectively no-op. (See http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.15.3 for what a unary + does.) += is an operator that increments the left-hand side of the assignment by the value of the right-hand side and assigns it back to the variable on the left-hand side. =+ is not an operator but, in fact, two operators: the assignment operator = and the unary plus +

Unary plus (+) against literal string

岁酱吖の 提交于 2019-11-27 06:41:09
问题 Today I wrote an expression: "<" + message_id + "@" + + ">" ^ | \____ see that extra '+' here! and got surprised that it actually compiled. (PS message_id is a QString , it would also work with an std::string ) I often do things like that, leave out a variable as I'm working and I expect the compiler to tell me where I'm still missing entries. The final would look something like this: "<" + message_id + "@" + network_domain + ">" Now I'd like to know why the + unary operator is valid against