operator-keyword

decltype and the scope operator in C++

谁说我不能喝 提交于 2019-11-26 21:49:01
问题 I need to obtain the type which was supplied when instantiating a template. Consider the following example: template <typename T> struct Foo { typedef T TUnderlying; }; static Foo<int> FooInt; class Bar { public: auto Automatic() -> decltype(FooInt)::TUnderlying { return decltype(FooInt)::TUnderlying(); } }; int main() { Bar bar; auto v = bar.Automatic(); return 0; } Problem with this code is using the scope operator together with decltype. Visual C++ 2010 complains like this: error C2039:

C++ Double Address Operator? (&&)

谁说我不能喝 提交于 2019-11-26 21:22:19
I'm reading STL source code and I have no idea what && address operator is supposed to do. Here is a code example from stl_vector.h : vector& operator=(vector&& __x) // <-- Note double ampersands here { // NB: DR 675. this->clear(); this->swap(__x); return *this; } Does "Address of Address" make any sense? Why does it have two address operators instead of just one? aschepler This is C++11 code. In C++11, the && token can be used to mean an "rvalue reference". Lexseal Lin && is new in C++11. int&& a means "a" is an r-value reference. && is normally only used to declare a parameter of a function

Prolog GNU - Univ operator? Explanation of it

做~自己de王妃 提交于 2019-11-26 20:40:17
问题 So the univ operator. I don't exactly understand it. For example this: foo(PredList,[H|_]) :- bar(PredList,H). foo(PredList,[_|T]) :- foo(PredList,T),!. bar([H|_],Item) :- G =.. [H,Item],G. bar([_|T],Item) :- bar(T,Item). What is this doing? This looks to see if another predicate is true. I don't understand what the ".." does. How would you rewrite this without the univ operator? 回答1: Univ ( =.. ) breaks up a term into a list of constituents, or constructs a term from such a list. Try: ?- f(x

What do >> and << mean in Python?

廉价感情. 提交于 2019-11-26 19:33:15
问题 I notice that I can do things like 2 << 5 to get 64 and 1000 >> 2 to get 250. Also I can use >> in print : print >>obj, "Hello world" What is happening here? 回答1: These are bitwise shift operators. Quoting from the docs: x << y Returns x with the bits shifted to the left by y places (and new bits on the right-hand-side are zeros). This is the same as multiplying x by 2**y . x >> y Returns x with the bits shifted to the right by y places. This is the same as dividing x by 2**y . 回答2: I think

What does the operator |= do in JavaScript?

£可爱£侵袭症+ 提交于 2019-11-26 19:23:39
问题 I found the following code in a JS project: var a = new Array(); a[0] = 0; for (var b = 0; b < 10; b++) { a[0] |= b; } What does the |= do in the body of the for loop? The code example is dubious, but has been presented here by V8 for an example of improved performance. Updated Example The above example is equivalent to var a = [15]; for most intents and purposes. A more realistic example for the |= operator would be to set up binary flags in a single variable, for example on a permission

Trouble with inheritance of operator= in C++

那年仲夏 提交于 2019-11-26 18:50:58
I'm having trouble with the inheritance of operator=. Why doesn't this code work, and what is the best way to fix it? #include <iostream> class A { public: A & operator=(const A & a) { x = a.x; return *this; } bool operator==(const A & a) { return x == a.x; } virtual int get() = 0; // Abstract protected: int x; }; class B : public A { public: B(int x) { this->x = x; } int get() { return x; } }; class C : public A { public: C(int x) { this->x = x; } int get() { return x; } }; int main() { B b(3); C c(7); printf("B: %d C: %d B==C: %d\n", b.get(), c.get(), b==c); b = c; // compile error // error:

How are Python in-place operator functions different than the standard operator functions?

廉价感情. 提交于 2019-11-26 17:53:11
Why isn't operator.iadd(x, y) equivalent to z = x; z += y ? And how does operator.iadd(x, y) differ from operator.add(x, y) ? From the docs : Many operations have an “in-place” version. The following functions provide a more primitive access to in-place operators than the usual syntax does; for example, the statement x += y is equivalent to x = operator.iadd(x, y). Another way to put it is to say that z = operator.iadd(x, y) is equivalent to the compound statement z = x; z += y. Related question , but I am not interested in Python class methods; just regular operators on built-in Python types.

PHP use string as operator

梦想与她 提交于 2019-11-26 17:51:29
问题 Say I have a string, $char. $char == "*". I also have two variables, $a and $b, which equal "4" and "5" respectively. How do I get the result of $a $char $b, ie 4 * 5 ? Thanks :) 回答1: You can use eval() as suggested by @konforce, however the safest route would be something like: $left = (int)$a; $right = (int)$b; $result = 0; switch($char){ case "*": $result = $left * $right; break; case "+"; $result = $left + $right; break; // etc } 回答2: safest method is a switch construct: function my

+ 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

Python - Human sort of numbers with alpha numeric, but in pyQt and a __lt__ operator [duplicate]

陌路散爱 提交于 2019-11-26 16:57:27
问题 This question already has an answer here: Does Python have a built in function for string natural sort? 16 answers I have data rows and wish to have them presented as follows: 1 1a 1a2 2 3 9 9.9 10 10a 11 100 100ab ab aB AB As I am using pyQt and code is contained within a TreeWidgetItem, the code I'm trying to solve is: def __lt__(self, otherItem): column = self.treeWidget().sortColumn() #return self.text(column).toLower() < otherItem.text(column).toLower() orig = str(self.text(column)