aggregate-initialization Questions
1
Solved
Consider the following setup:
struct MyInt {
template<class T>
operator T() = delete;
operator int(){ return 42; }
};
struct TestCArr {
int arr[2];
};
struct TestStdArr {
std::array&l...
Smilax asked 6/9 at 16:45
2
Solved
Consider the following code:
struct Foo{
std::string s1;
std::string s2;
};
int main(){
Foo f{.s1 = "s1", .s2 = f.s1 + "s2"};
std::cout << "s1='" << f...
Iago asked 3/11, 2023 at 11:13
3
Solved
I always thought that aggregate initialization was to save coders from writing custom constructors. However, this seems to have "sneaked in" a "security by-pass" for private con...
Trant asked 5/1, 2021 at 14:23
4
Solved
If I don't define a constructor in a struct, I can initialize it by just picking a certain value like this:
struct Foo {
int x, y;
};
Foo foo = {.y = 1};
But if I add new default constructor the...
Dorelle asked 17/7, 2022 at 14:43
2
Solved
In regards to C++17; GCC, Clang, and MSVC consider a trival class type not to be constructible by any of its data member types. Since C++20, GCC and MSVC changed this, allowing the example below to...
Collis asked 11/6, 2022 at 16:2
7
Solved
class C
{
public:
C() : arr({1,2,3}) //doesn't compile
{}
/*
C() : arr{1,2,3} //doesn't compile either
{}
*/
private:
int arr[3];
};
I believe the reason is that arrays can be initialized on...
Tachycardia asked 30/10, 2010 at 8:48
3
Solved
The following code is returning the compilation error below. I'm stuck understanding how there are too many initializers. This code works using vector<X>. Does anyone know why the error is be...
Salmonella asked 7/6, 2020 at 21:44
1
Solved
I am facing a relatively tricky situation here that seemed quite easy at first sight. After moving those three members from the parent class Parent to its child class Child it seems that I'm no lon...
Almondeyed asked 31/3, 2022 at 14:41
1
I am trying to write mostly a reimplementation of std::array. But, I am not very happy about the rules for aggregate initialization (and some other minor details) and thus do not want to use it.
Th...
Slemmer asked 7/2, 2022 at 10:42
2
Solved
In the following code there is an initialization of A<T> objects with template argument deduction using two forms distinct by the type of braces:
template<typename T>
struct A{ T x; };
...
Particle asked 14/11, 2021 at 8:34
1
Solved
Assume the following code snippet:
#include <optional>
struct MyStruct
{
// MyStruct(int a) : a(a) {}
int a;
};
int main()
{
std::optional<MyStruct> ms1 = std::make_optional<MySt...
Averell asked 13/10, 2021 at 6:1
1
C++20 introduced support for designated initializers.
In g++ with -std=c++17, one can use designated initializers and as long as you don't leave any out, it will compile without any errors or warni...
Synergistic asked 1/9, 2021 at 7:22
1
Solved
In the following program the union U has two fields a and b, each with distinct default value. If one creates a variable of type U using aggregate initialization {} what are the value and the activ...
Marlea asked 22/8, 2021 at 12:40
2
Solved
Please consider the code with aggregate struct B having a field of class A with a private constructor:
class A { A(int){} friend struct B; };
struct B { A a{1}; };
int main()
{
B b; //ok everywhe...
Awaken asked 8/8, 2021 at 9:13
1
Solved
Please consider this short code example:
#include <iostream>
struct A
{
A() { std::cout << "A() "; }
~A() { std::cout << "~A() "; }
};
struct B { const A &...
Blanco asked 13/7, 2021 at 9:51
1
Solved
As far as I understand, the following program should work in C++20 mode:
#include <vector>
struct B{ int a0, a1; };
int main()
{
std::vector<B> bs;
bs.emplace_back( 0, 0 );
}
And ...
Inchmeal asked 16/6, 2021 at 16:39
1
Solved
How to in-place construct an optional aggregate? It seems I can only construct an optional single thing, and not an optional aggregate of things.
#include <optional>
#include <iostream>...
Shuma asked 28/4, 2021 at 11:44
2
Solved
See this example: https://godbolt.org/z/5PqYWP
How come this array of pairs can't be initialized in the same way as a vector of pairs?
#include <vector>
#include <array>
int main()
{
...
Socrates asked 8/12, 2020 at 11:25
1
Solved
With C++20, it is possible to have deduction guidelines generated for an alias template (See section "Deduction for alias templates" at https://en.cppreference.com/w/cpp/language/class_te...
Brunabrunch asked 21/11, 2020 at 3:0
1
Solved
When using aggregate / designated initialization of a struct it is possible to refer to another field like this:
#include <stdio.h>
int main()
{
struct
{
int a;
int b;
}
s =
{
.a =...
Swifter asked 12/11, 2020 at 19:1
2
Solved
I'm using the clang compiler (c++ 11 I think) that comes with RAD studio 10.2. By mistake I discovered today that the first n members of a struct or array can be assigned using the usual curly brac...
Myosotis asked 17/2, 2020 at 16:10
4
Solved
Consider the following code:
struct A
{
// No data members
//...
};
template<typename T, size_t N>
struct B : A
{
T data[N];
}
This is how you have to initialize B: B<int, 3> b =...
Scythe asked 27/12, 2019 at 9:27
1
P1008 ("Prohibit aggregates with user-declared constructors") has become part of the C++20 standard, in order to prevent surprising behavior when using aggregate initialization:
struct X {
int i{...
Straley asked 13/11, 2019 at 15:48
1
The code below prints garbage (or zeroes) if compiled with VC++ 2017 and "1122" if compiled with GCC or Clang (https://rextester.com/JEV81255). Is it bug of VC++ or I'm missing something here?
#in...
Porism asked 4/9, 2019 at 10:41
3
Solved
Let's say I have a type and I want to make its default constructor private. I write the following:
class C {
C() = default;
};
int main() {
C c; // error: C::C() is private within this context ...
Certitude asked 3/6, 2016 at 15:27
1 Next >
© 2022 - 2024 — McMap. All rights reserved.