Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assignment vs constructor in C++ [duplicate]

I wrote some code S s; ... s = {};, expecting it to end up the same as S s = {};. However it didn't. The following example reproduces the problem:

#include <iostream>

struct S
{
    S(): a(5) { }
    S(int t): a(t) {}

    S &operator=(int t)  { a = t; return *this; }
    S &operator=(S const &t) = default;

    int a;
};

int main()
{
    S s = {};

    S t;
    t = {};

    std::cout << s.a << '\n';
    std::cout << t.a << '\n';
}

The output is:

5
0

My questions are:

  1. Why is operator=(int) selected here, instead of "ambiguous" or the other one?
  2. Is there a tidy workaround, without changing S?

My intent is s = S{}; . Writing s = {}; would be convenient if it worked. I'm currently using s = decltype(s){}; however I'd prefer to avoid repeating the type or the variable name.

like image 896
M.M Avatar asked Nov 04 '15 00:11

M.M


People also ask

Are copy constructors deep copies?

The default copy constructor and default assignment operators do shallow copies, which is fine for classes that contain no dynamically allocated variables. Classes with dynamically allocated variables need to have a copy constructor and assignment operator that do a deep copy.

What is a copy assignment constructor?

Copy constructor. Assignment operator. It is called when a new object is created from an existing object, as a copy of the existing object. This operator is called when an already initialized object is assigned a new value from another existing object. It creates a separate memory block for the new object.

What is the difference between the copy constructor and the assignment operator ie when will each of these functions be called and why?

A copy constructor is an overloaded constructor whereas an assignment operator is a bitwise operator. Using copy constructor you can initialize a new object with an already existing object. On the other hand, an assignment operator copies one object to the other object, both of which are already in existence.

What is assignment and copy initialization?

Direct Initialization or Assignment Operator (Syntax) This assigns the value of one object to another object both of which are already exists. Copy initialization is used when a new object is created with some existing object. This is used when we want to assign existing object to new object.


1 Answers

Why is operator=(int) selected here, instead of "ambiguous" or the other one?

{} to int is the identity conversion ([over.ics.list]/9). {} to S is a user-defined conversion ([over.ics.list]/6) (technically, it's {} to const S&, and goes through [over.ics.list]/8 and [over.ics.ref] first before coming back to [over.ics.list]/6).

The first wins.

Is there a tidy workaround?

A variation of the trick std::experimental::optional pulls to make t = {} always make t empty. The key is to make operator=(int) a template. If you want to accept int and only int, then it becomes

template<class Int, std::enable_if_t<std::is_same<Int, int>{}, int> = 0>
S& operator=(Int t) { a = t; return *this; }

Different constraints can be used if you want to enable conversions (you'd probably also want to take the argument by reference in that case).

The point is that by making the right operand's type a template parameter, you block t = {} from using this overload - because {} is a non-deduced context.

...without changing S?

Does template<class T> T default_constructed_instance_of(const T&) { return {}; } and then s = default_constructed_instance_of(s);count?

like image 108
T.C. Avatar answered Sep 28 '22 00:09

T.C.