Initializes an object from explicit set of constructor arguments.
Questions tagged [direct-initialization]
14 questions
20
votes
1 answer
Why does this code generate different output for different compilers?
I have the following code:
#include
#include
struct C {
int a;
C() : a(0) {}
C(int a) : a(a) {}
};
std::ostream &operator<<(std::ostream &os, const C &c) {
os << c.a;
return os;
}
using T = std::vector;
int…

Matvei
- 217
- 3
5
votes
2 answers
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 }; }
int main()
{
i_take_an_S(i_return_an_S());
}
With the…

Ruperrrt
- 489
- 2
- 13
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
3
votes
1 answer
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
struct A {
int v = 0;
A() {}
A(const A &) : v(1) {}
};
int main() {
A a({A{}});
std::cout << a.v;
}
MSVC and GCC…

Fedor
- 17,146
- 13
- 40
- 131
2
votes
1 answer
Direct initialization != copy initialization when converting from different type?
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 copy initialize (using = ) and only compiles when I…

glades
- 3,778
- 1
- 12
- 34
2
votes
1 answer
C++ object construction -- direct initialization versus using the '=' operator, are they equivalent?
In C++, are these two styles of initializing a class-object functionally equivalent, or are there some situations where they might have differing semantics and generate different code?
SomeClass foo(1,2,3);
vs
auto foo = SomeClass(1,2,3);

Jeremy Friesner
- 70,199
- 15
- 131
- 234
1
vote
2 answers
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 are considered using the rules…

mada
- 1,646
- 1
- 15
1
vote
1 answer
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); // direct-initialization or…

mada
- 1,646
- 1
- 15
1
vote
1 answer
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
- 35
- 3
1
vote
1 answer
C++ Stringstream initializations
I have two code snippets:
This does not compile:
std::string reverseSentence(std::string sentence) {
std::stringstream stream = sentence;
}
This does:
std::stringstream stream (sentence);
It's my understanding that T foo = expr is T…

nz_21
- 6,140
- 7
- 34
- 80
0
votes
0 answers
Variable declaration by calling its constructor
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 use of auto self = shared_from_this();, which…

medihack
- 16,045
- 21
- 90
- 134
0
votes
1 answer
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 parenthesized list of expressions
It is direct…

Kevin eyeson
- 375
- 4
- 8
0
votes
1 answer
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 lst1{};
std::initializer_list lst2{lst1}; // error
}
The compiler (gcc) gives me the following error:
error: could not…

mada
- 1,646
- 1
- 15
0
votes
1 answer
Direct initialization gives an error when initializing class types
struct Alien {
int id;
char const* name;
Alien() = default;
Alien(int id, char const* name) : id{id}, name{name} {}
Alien(int id): id{id} {}
};
struct Spaceship {
Alien alien1;
Spaceship() = default;
…

Vegeta
- 461
- 2
- 6