Skip to main content
The 2024 Developer Survey results are live! See the results

Questions tagged [direct-initialization]

Initializes an object from explicit set of constructor arguments.

direct-initialization
5 votes
1 answer
154 views

Why does direct initialization use a const lvalue reference qualified conversion function?

I have two classes st and foo: struct st { st() = default; st(const st&) { std::cout << "copy ctor." << std::endl; } st(st&&) { std::...
ValueError's user avatar
2 votes
1 answer
72 views

Why does direct-initialization not work for std::views::iota?

Why is this an illegal declaration? > std::views::iota r(0, 10); error: expected ‘;’ before ‘r’ but this works > auto r = std::views::iota(0, 10);
Cherif's user avatar
  • 85
3 votes
1 answer
51 views

Why does direct initializing a base class member from a prvalue require a copy or move constructor?

It is well known that for a class with deleted copy and move constructors, it is still possible to direct initialize an object from a prvalue such as the return value of a function. For example, this ...
Ari's user avatar
  • 33
1 vote
1 answer
90 views

Why warning instead of hard error on ill formed program [duplicate]

I'm aware that uniform initialization using braced-init list don't allow narrow conversion (ex double -> int) but member initialization using init list allows narrow conversion. struct Mem1 { ...
ssh's user avatar
  • 69
20 votes
1 answer
863 views

Why does this code generate different output for different compilers?

I have the following code: #include <iostream> #include <vector> struct C { int a; C() : a(0) {} C(int a) : a(a) {} }; std::ostream &operator<<(std::ostream &os, ...
Matvei's user avatar
  • 217
0 votes
0 answers
61 views

Variable declaration by calling its constructor [duplicate]

In some Boost ASIO example, I found the line auto self(shared_from_this());. self doesn't seem to be declared anywhere and doesn't seem to be a reserved keyword. In another example, I also found the ...
medihack's user avatar
  • 16.5k
0 votes
1 answer
61 views

In C++, under the case `T object(other);`, is it direct initialization or copy initialization?

Assume I have this code T object(other); It is direct initialization or copy initialization? Based on the rule of direct initialization : T object ( arg ); initialization with a nonempty ...
Kevin eyeson's user avatar
2 votes
1 answer
104 views

Direct initialization != copy initialization when converting from different type? [duplicate]

I always thought direct initialization and copy initialization for types T that do not match the class type are absolutely equal. Yet I seem to be mistaken. The following code doesn't compile if I ...
glades's user avatar
  • 4,475
0 votes
1 answer
372 views

How to list-initialize an initializer_list from another initializer_list

When I'm trying to compile the following code, the compiler complains: int main(void) { std::initializer_list<int> lst1{}; std::initializer_list<int> lst2{lst1}; // error } The ...
mada's user avatar
  • 1,932
5 votes
2 answers
109 views

Direct initialization with prvalue: Bug in MSVC?

Consider the following code: struct S { S(int, double) {} explicit S(const S&) {} explicit S(S&&) {} }; void i_take_an_S(S s) {} S i_return_an_S() { return S{ 4, 2.0 }; } ...
Ruperrrt's user avatar
  • 509
1 vote
2 answers
140 views

In which case(s) user-defined conversions are not considered during reference initialization?

Consider a case where we've reached into bullet [dcl.init.ref]/(5.4.1) during reference binding: (5.4.1) If T1 or T2 is a class type and T1 is not reference-related to T2, user-defined conversions ...
mada's user avatar
  • 1,932
4 votes
1 answer
208 views

Copy-elision in direct initialization from braced-init-list

In the following program the object A a is directly initialized from braced-init-list {A{}}: #include <iostream> struct A { int v = 0; A() {} A(const A &) : v(1) {} }; int main(...
Fedor's user avatar
  • 19.3k
2 votes
1 answer
291 views

Is direct-initialization equivalent to direct-list-initialization?

I have the following example: struct S{ int x, y; } S s1{1}; // direct-initialization or direct-list-initialization ? S s2{1, 2}; // direct-initialization or direct-list-initialization ? S s3(1)...
mada's user avatar
  • 1,932
4 votes
1 answer
1k views

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::...
Ruperrrt's user avatar
  • 509
1 vote
1 answer
58 views

Initialization in C++

What is the difference between direct initialization and uniform initialization in C++? What is the difference between writing int a{5}; // Uniform and int a(5); // Direct
Ash Ketchum's user avatar

15 30 50 per page