Questions tagged [copy-initialization]

Initializes an object from another object

52 questions
21
votes
2 answers

When should you use direct initialization and when copy initialization?

Is it simply preference or are there specific instances where one is necessary over another? I'm refering to the following variants for initialization T t(e); // direct initialization T t = e; // copy initialization
14
votes
2 answers

Copy initialization of the form '= {}'

Given the following: #include class X; class Y { public: Y() { printf(" 1\n"); } // 1 // operator X(); // 2 }; class X { public: X(int) {} X(const Y& rhs) { printf(" 3\n"); } // 3 X(Y&& rhs) { printf(" 4\n"); } …
Rich
  • 185
  • 1
  • 6
13
votes
1 answer

Which of these conversions should be ambiguous?

I have some code like the following: class bar; class foo { public: operator bar() const; }; class bar { public: bar(const foo& foo); }; void baz() { foo f; bar b = f; // [1] const foo f2; bar b2 = f2; // [2] } GCC…
11
votes
2 answers

Why does copy initializaton require destructor in C++17 with guaranteed move/copy elision?

The following code compiles with MSVC (/permissive-) and fails to compile with GCC/Clang for m_ptr1 and m_ptr2. #include struct ForwardDeclared; class A { public: explicit A(); ~A(); private: …
JVApen
  • 11,008
  • 5
  • 31
  • 67
10
votes
1 answer

Does copy list initialization invoke copy ctor conceptually?

Before C++11, we can do copy initialization by writing something like A a = 1; which is more or less equivalent to A a = A(1);. That is, a temporary is first created and then a copy ctor is invoked. Regardless of copy elision, this must be so…
Lingxi
  • 14,579
  • 2
  • 37
  • 93
9
votes
1 answer

Reference binding through ambiguous conversion operator

#include using namespace std; struct CL2 { CL2(){} CL2(const CL2&){} }; CL2 cl2; struct CL1 { CL1(){} operator CL2&(){cout<<"operator CL2&"; return cl2;} operator const CL2&(){cout<<"operator const CL2&"; return…
Denis
  • 2,786
  • 1
  • 14
  • 29
8
votes
6 answers

Strange behavior of copy-initialization, doesn't call the copy-constructor!

I was reading the difference between direct-initialization and copy-initialization (§8.5/12): T x(a); //direct-initialization T y = a; //copy-initialization What I understand from reading about copy-initialization is that it needs accessible &…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
8
votes
1 answer

Why is copy initialization the way it is? Why require the copy constructor?

Possible Duplicate: What’s the motivation behind having copy and direct initialization behave differently? And by copy initialization, I mean like so: struct MyStruct { MyStruct(int) {} MyStruct(const MyStruct&) {} }; MyStruct s = 5; //…
Cornstalks
  • 37,137
  • 18
  • 79
  • 144
6
votes
1 answer

Can not-copyable class be caught by value in C++?

In the next program, struct B with deleted copy-constructor is thrown and caught by value: struct B { B() = default; B(const B&) = delete; }; int main() { try { throw B{}; } catch( B ) { } } Clang rejects the code…
Fedor
  • 17,146
  • 13
  • 40
  • 131
6
votes
2 answers

copy initialization - conversion from 'int' to non-scalar type

I would like to know how I should define the class my_int so that the cast from int to std::complex< my_int > is done by the compiler instead of manually by me. The following program does not compile if 4 is not casted to "my_int" // Example…
rual93
  • 553
  • 4
  • 11
6
votes
1 answer

This is not copy-initializing, or is it?

In the following code I am not allowed to declare an explicit ctor because the compiler says I am using it in a copy-initializing context (clang 3.3 and gcc 4.8). I try to prove the compilers wrong by making the ctor non explicit and then declaring…
Patrick Fromberg
  • 1,313
  • 11
  • 37
6
votes
2 answers

Is it possible to infer template parameters of tuple from brace-type initialization?

In this example, is it possible to allow the deduction of the template parameters type of the tuple? #include #include template void fun(std::tuple t, std::string other){} int main(){ …
alfC
  • 14,261
  • 4
  • 67
  • 118
4
votes
1 answer

Why smart pointer type member variable can't be initialized at the declaring place in a class?

When I want to add a member variable with smart pointer type to a class, I found that it can't be initialized at the declaring place: class Foo { public: std::shared_ptr intSharedPtr = new int; // not ok Foo() {} }; But I can do…
4
votes
1 answer

Initialization in return statements of functions that return by-value

My question originates from delving into std::move in return statements, such as in the following example: struct A { A() { std::cout << "Constructed " << this << std::endl; } A(A&&) noexcept { std::cout << "Moved " << this << std::endl; } …
Ruperrrt
  • 489
  • 2
  • 13
4
votes
1 answer

Why does copy initialization of my class not work with string_view using string literals?

I have the following code: #include class Foo { public: Foo(std::string_view) {} }; When I do this, everything compiles fine (using clang v8, C++17): Foo f("testing"); However, if I use copy initialization it fails: Foo f =…
void.pointer
  • 24,859
  • 31
  • 132
  • 243
1
2 3 4