initialization-order Questions
2
Solved
Suppose that I have some typeclass
trait FooBar[X]
and an instance of FooBar[Int]:
given intIsFooBar: FooBar[Int] = new FooBar {}
Now, suppose that I have an interface Intf that has some member t...
Virgil asked 21/3, 2023 at 21:31
1
Solved
The code below is in the same translation unit and A::v is defined after x, why A::v is not initialized to "ok" ?
#include <string>
#include <iostream>
std::string foo() {
r...
Carrew asked 26/9, 2023 at 9:38
1
I'm aware this question has been asked many times, but this seems to be a slightly different variation which I can't figure out.
Consider the following code:
#include <cstdio>
struct TestVal...
Erwinery asked 18/5, 2022 at 9:25
1
Solved
I know that in C++ the declaration of members in the class header defines the initialization order. Can you tell me why C++ choose this design? Are there any benefits to force the initialize order ...
Haihaida asked 30/6, 2021 at 5:21
1
Solved
Generally it is said that the destructors of static objects are called in the reverse order of the constructors. As I understand, constinit objects are initialized at compile time, so their destruc...
Doublespace asked 16/7, 2020 at 5:40
1
Solved
Following code
#include <iostream>
struct A {
A() {
std::cout << std::endl;
}
};
struct B {
static inline A a;
};
int main() {
}
succeeds after compiling with gcc, but crashes w...
Gary asked 5/7, 2020 at 7:32
1
Solved
C++ standards (earlier than C++17, at least) have said this about initialization order.
Objects with static storage duration defined in namespace scope in the same translation unit and dynamica...
Fairhaired asked 9/8, 2019 at 21:52
1
Solved
cppreference says about std::atexit :
The functions may be called concurrently with the destruction of the objects with static storage duration and with each other, maintaining the guarantee tha...
Nancee asked 16/5, 2018 at 19:53
2
Solved
Consider:
int f () {
static int i = 0;
return i++;
}
struct Test {
int a, b;
Test () : a(f()), b(f()) {}
};
Test t;
I know that a is initialized before b due to the order of their declarat...
Sclerosis asked 18/4, 2017 at 18:39
5
Solved
I have some doubts about construction and initialization order guarantees in C++. For instance, the following code has four classes X, Y, Z and W. The main function instantiates an object of class ...
Shaffert asked 25/3, 2010 at 15:50
4
Solved
I am looking for a good solution for a decentralized module registration.
I do not want a single unit that uses all module units of the project, but I would rather like to let the module units reg...
Grummet asked 29/3, 2014 at 15:30
6
Solved
When I use static variables in C++, I often end up wanting to initialize one variable passing another to its constructor. In other words, I want to create static instances that depend on each other...
Unbeatable asked 17/6, 2009 at 8:0
2
Solved
During my research into the best way to build a Singleton in C# I stumbled across the following article where there is a brief mention that in C++
"The C++ specification left some ambiguity aro...
Acatalectic asked 9/7, 2012 at 9:50
4
Solved
I have this question, which i thought about earlier, but figured it's not trivial to answer
int x = x + 1;
int main() {
return x;
}
My question is whether the behavior of the program is defined...
Chemaram asked 22/7, 2010 at 12:57
3
Solved
There is a simple and well-known pattern to avoid the static initialization fiasco, described in section 10.13 of the C++ FAQ Lite.
In this standard pattern, there is a trade-off made in that eith...
Radiomicrometer asked 29/6, 2010 at 17:26
2
Solved
Initialization order of free objects is undefined in C++. But what about the following?
namespace foo {
char const* str = "hey";
struct A {
A() { cout << str; }
} obj;
}
Is this still ...
Elledge asked 26/4, 2009 at 11:39
1
© 2022 - 2024 — McMap. All rights reserved.