unary-operator

Does the unary + operator have any practical use?

我与影子孤独终老i 提交于 2019-11-27 06:34:16
问题 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? 回答1: char ch = 'a'; std::cout << ch << '\n'; std::cout << +ch << '\n';

Prefix form of unary operator in Haskell

本小妞迷上赌 提交于 2019-11-27 05:38:01
In GHCi: Prelude> (+3) 2 5 Prelude> (*3) 2 6 Prelude> (/3) 2 0.6666666666666666 Prelude> (-3) 2 No instance for (Num (t -> t1)) arising from the literal 3' at <interactive>:1:2 Possible fix: add an instance declaration for (Num (t -> t1)) In the expression: 3 In the expression: (- 3) 2 In the definition of it': it = (- 3) 2 How can I correct the last one to make it return -1? Haskell's grammar doesn't allow you to use - like that. Use the subtract function instead: (subtract 3) 2 Travis Brown As a footnote to grddev's answer , here's the relevant paragraph from the Haskell 98 Report : The

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

ぐ巨炮叔叔 提交于 2019-11-26 21:49:00
问题 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? 回答1: Perhaps it's

Does it make sense for unary operators to be associative?

左心房为你撑大大i 提交于 2019-11-26 21:47:12
问题 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? 回答1: It's just an artefact of the way that the associativity is derived from the grammar. The reason that addition is left

Explain +var and -var unary operator in javascript

余生长醉 提交于 2019-11-26 19:01:25
I'm trying to understand unary operators in javascript, I found this guide here http://wiki.answers.com/Q/What_are_unary_operators_in_javascript most of it makes sense but what I don't understand is how the following examples would be used in an actual code example: +a; -a; To my understanding the +a; is meant to make the variable the positive value of a and the -a; is meant to make the variable the negative value of a. I've tried a number of examples like: a = -10; a = +a; document.writeln(a); And the output is still -10; I've also tried: a = false; a = +a; document.writeln(a); And the output

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

删除回忆录丶 提交于 2019-11-26 17:21:40
问题 This question already has answers here : Closed 8 years ago . 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

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

北慕城南 提交于 2019-11-26 17:12:11
问题 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. 回答1: 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. 回答2: According to MDN: The unary plus operator

What is the difference between += and =+?

六眼飞鱼酱① 提交于 2019-11-26 16:55:06
问题 What is the difference between += and =+? Specifically, in java, but in general also. 回答1: 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.) 回答2: += 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

What does the unary plus operator do?

扶醉桌前 提交于 2019-11-26 13:04:53
What does the unary plus operator do? There are several definitions that I have found ( here and here ) but I still have no idea what it would be used for. It seems like it doesn't do anything but there has be a reason for it, right? It's there to be overloaded if you feel the need; for all predefined types it's essentially a no-op. The practical uses of a no-op unary arithmetic operator are pretty limited, and tend to relate to the consequences of using a value in an arithmetic expression, rather than the operator itself. For example, it can be used to force widening from smaller integral

Prefix form of unary operator in Haskell

我的梦境 提交于 2019-11-26 11:42:11
问题 In GHCi: Prelude> (+3) 2 5 Prelude> (*3) 2 6 Prelude> (/3) 2 0.6666666666666666 Prelude> (-3) 2 No instance for (Num (t -> t1)) arising from the literal 3\' at <interactive>:1:2 Possible fix: add an instance declaration for (Num (t -> t1)) In the expression: 3 In the expression: (- 3) 2 In the definition of it\': it = (- 3) 2 How can I correct the last one to make it return -1? 回答1: Haskell's grammar doesn't allow you to use - like that. Use the subtract function instead: (subtract 3) 2 回答2: