All Questions
Tagged with brace-initialization or list-initialization
367 questions
Best practices
2
votes
6
replies
241
views
Forwarding C-style struct matrix to constructors of same-size matrix of objects
Suppose I have a C-style matrix of elements
typedef struct _RAW_DATA{
int a;
int b;
}RAW_DATA;
inside a templated class, that defines the size of the matrix:
template<size_t TDim>
...
8
votes
1
answer
251
views
Is this list-initialization well-formed in C++20 and later?
Consider:
struct A {
A();
template<class T> A(T&);
};
struct B {
B();
B(B&);
};
struct C {
A a;
B b;
};
const C c1;
C c2{c1};
All compilers agree that the ...
4
votes
1
answer
174
views
Why does template deduction for a nested std::array with one element result in a one dimensional std::array?
The following code compiles successfully with clang and gcc. Naively, I would have expected a2 to have type std::array<std::array<int, 1>, 1>.
#include <array>
auto a1 = std::array{...
1
vote
1
answer
225
views
Why ternary operator cannot be used for this statement? [duplicate]
This code gives me compiler error: "expected an expression".
std::array< std::string, 3 > candidates = useOutname ? { "%O.log", "%O_.log", "_%O.log" } : { ...
0
votes
0
answers
58
views
Is it undefined behavior to access the elements of a list-initialized std::initializer_list after it has been destroyed? [duplicate]
The following code when compiled with -fsanitize=address emits a stack-use-after-scope error in GCC 12.3 and Clang 20.1, but not GCC 14.2.
#include <functional>
#include <cstdint>
#include ...
-5
votes
2
answers
375
views
Why does C zero-fill remaining elements when partially initializing an array?
I declared an array in C as:
#include <stdio.h>
int main() {
int arr[5] = { 1, 2, 3 };
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
...
1
vote
1
answer
82
views
Is there any way to prevent this conflict in function overload?
In the following code I'm trying to call the constructor that takes a std::uint64_t:
#include <initializer_list>
#include <cstdint>
template <typename T>
struct MYFOO
{
MYFOO(...
1
vote
1
answer
136
views
MSVC default memeber initialization with aggregate initialization error C2440
I'm having some problems using aggregate initializers to initialize a POD with default member initializers.
According to this article, this code snippet should work:
struct Something
{
int x;
...
2
votes
1
answer
67
views
Retaining braced initialization with a deleted default constructor
I'd like to be able to have an aggregate struct type that cannot be accidentally left uninitialized.
Currently, with the following code:
struct Foo {
int a;
int b;
};
Foo foo;
foo is left ...
0
votes
1
answer
93
views
How to use default member initializer if `std::initializer_list` ctor exists in C++11?
Consider the following code:
#include <iostream>
struct A {
A(int, int) {
std::cout << "A::A(int, int)\n";
}
A(std::initializer_list<int>) {
...
3
votes
1
answer
111
views
How to convert a copy initialization into a direct list initialization in clang-tidy check?
I would like to write a clang-tidy check that finds field declarations with copy initialization (ICIS_CopyInit) and can change them into direct list initializations (ICIS_ListInit).
There is a way to ...
-1
votes
1
answer
89
views
Unexpected concurrency in async method calls in List initialization [closed]
I wrote a method that populates a list with objects returned by an asynchronous method.
public async Task<ObjectToReturn> GetObjectToReturnAsync()
{
var objectToReturn = new ObjectToReturn
...
-1
votes
2
answers
188
views
Reference initialization using list initialization accepted by gcc and msvc but rejected by clang
Consider the following example that compiles with gcc, msvc and edg but is rejected by clang. Demo
struct X
{
public:
X(){}
};
struct Y : X
{
};
struct Z
{
public:
operator ...
2
votes
2
answers
154
views
Copy-list-initialization from empty braces with explicit default constructor
Why do seemingly all compilers reject the following program? (https://godbolt.org/z/EK8zW34nY)
struct A
{
explicit A() {}
};
int main()
{
A a = {};
}
a should be value-initialized per [dcl....
3
votes
1
answer
145
views
Aggregate Initialization when initialization values is fewer than the number of members
As per the document, which states that[emphasise mine]:
Missing initializers in an initializer list
If an aggregate is initialized but the number of initialization values
is fewer than the number of ...