assignment-operator

ambiguity of `<<-` when defining it for `x < y <- z`

最后都变了- 提交于 2019-12-06 00:53:21
问题 @g-grothendieck's answer to this question inspired me to play with some assignment functions such as ==<- or ><- . See the following : `><-` <- function(e1,e2,value) replace(e1, e1 > e2, value) x <- c(5,15) x > 10 <- 42 x # [1] 5 42 I can also define it for < : `<<-` <- function(e1, e2, value) replace(e1, e1 < e2, value) x <- c(5,15) x < 10 <- 42 x # [1] 42 15 But the problem is that now the <<- operator is redefined and this doesn't work anymore : x <<- "hello" Error in replace(e1, which(e1

Initialization different from assignment?

怎甘沉沦 提交于 2019-12-05 23:14:33
1.char str[] = "hello"; //legal 2.char str1[]; str1 = "hello"; // illegal I understand that "hello" returns the address of the string literal from the string literal pool which cannot be directly assigned to an array variable. And in the first case the characters from the "hello" literal are copied one by one into the array with a '\0' added at the end. Is this because the assignment operator "=" is overloaded here to support this? I would also like to know other interesting cases wherein initialization is different from assignment. You cannot think of it as overloading (which doesn't exist in

Default assignment operator in inner class with reference members

馋奶兔 提交于 2019-12-05 22:25:48
问题 I've run into an issue I don't understand and I was hoping someone here might provide some insight. The simplified code is as follows (original code was a custom queue/queue-iterator implementation): class B { public: B() {}; class C { public: int get(); C(B&b) : b(b){}; private: B& b; }; public: C get_c() { return C(*this); } }; int main() { B b; B::C c = b.get_c(); c = b.get_c(); return EXIT_SUCCESS; } This, when compiled, gives me the following error: foo.cpp: In member function 'B::C& B:

JavaScript evaluation order when assigning

て烟熏妆下的殇ゞ 提交于 2019-12-05 18:29:43
At what point does JavaScript determine the left-hand side of an assignment — is it before or after the right-hand side is evaluated? For example, what does this code do? var arr = [{thing:1},{thing:2},{thing:3},{last:true}]; arr[arr.length - 1].newField = arr.pop(); The left-hand side of an assignment operator is evaluated first. The specification for this as of ES2015 can be found in the "Runtime Semantics: Evaluation" portion of the "Assignment Operators" section and can be very roughly summarized as: Evaluate the LHS to determine the reference Evaluate the RHS to determine the value Assign

Why doesn't a derived class use the base class operator= (assignment operator)?

本秂侑毒 提交于 2019-12-05 15:18:53
问题 Following is a simplified version of an actual problem. Rather than call Base::operator=(int) , the code appears to generate a temporary Derived object and copy that instead. Why doesn't the base assignment operator get used, since the function signature seems to match perfectly? This simplified example doesn't display any ill effects, but the original code has a side-effect in the destructor that causes all kinds of havoc. #include <iostream> using namespace std; class Base { public: Base()

C++17 sequencing in assignment: still not implemented in GCC?

眉间皱痕 提交于 2019-12-05 14:32:35
I tried the following code as a naive attempt to implement swapping of R and B bytes in an ABGR word #include <stdio.h> #include <stdint.h> uint32_t ABGR_to_ARGB(uint32_t abgr) { return ((abgr ^= (abgr >> 16) & 0xFF) ^= (abgr & 0xFF) << 16) ^= (abgr >> 16) & 0xFF; } int main() { uint32_t tmp = 0x11223344; printf("%x %x\n", tmp, ABGR_to_ARGB(tmp)); } To my surprise this code "worked" in GCC in C++17 mode - the bytes were swapped http://coliru.stacked-crooked.com/a/43d0fc47f5539746 But it is not supposed to swap bytes! C++17 clearly states that the RHS of assignment is supposed to be [fully]

boost::optional not letting me reassign const value types

让人想犯罪 __ 提交于 2019-12-05 08:20:05
It seems to me there should be four variants of boost::optional optional<Foo> => holds a mutable Foo and can be reassigned after initialization optional<Foo const> const => holds a const Foo and can't be reassigned after initialization optional<Foo> const => (should?) hold a mutable Foo but can't be reassigned after initialization optional<Foo const> => (should?) hold a const Foo and can be reassigned after initialization The first 2 cases work as expected. But the optional<Foo> const dereferences to a const Foo, and the optional<Foo const> doesn't allow reassignment after initialization (as

is_assignable and std::unique_ptr

ぐ巨炮叔叔 提交于 2019-12-05 07:26:15
Here is a test file from gcc, live demo struct do_nothing { template <class T> void operator()(T*) {} }; int main() { int i = 0; std::unique_ptr<int, do_nothing> p1(&i); std::unique_ptr<int> p2; static_assert(!std::is_assignable<decltype(p2), decltype(p1)>::value, ""); // note ! here. } std::is_assignable If the expression std::declval<T>() = std::declval<U>() is well-formed in unevaluated context, provides the member constant value equal true. Otherwise, value is false. Access checks are performed as if from a context unrelated to either type. std::declval : template<class T> typename std:

About Scala's assignments and setter methods

牧云@^-^@ 提交于 2019-12-05 05:09:57
问题 Edit: The bug which prompted this question has now been fixed. In the Scala Reference, I can read (p. 86): The interpretation of an assignment to a simple variable x = e depends on the definition of x. If x denotes a mutable variable, then the assignment changes the current value of x to be the result of evaluating the expression e. The type of e is expected to conform to the type of x. If x is a parameterless function defined in some template, and the same template contains a setter function

c++: cast operator vs. assign operator vs. conversion constructor priority

北城余情 提交于 2019-12-05 03:31:15
Let's have this code: Test1 t1; Test2 t2; t1 = t2; I believe there are three (or more?) ways how to implement t1 = t2 to overload assign operator in Test1 to overload type cast operator in Test2 to create Test1(const Test2&) conversion constructor According to my GCC testing, this is the priority of what is used: assign operator conversion constructor and type cast operator (ambiguous) const conversion constructor and const type cast operator (ambiguous) Please help me understand why this priority. I use this code for testing (uncomment some lines to try out) struct Test2; struct Test1 { Test1