static-initialization

Is initialization of local static function-object thread-safe?

做~自己de王妃 提交于 2019-12-05 23:18:51
问题 The following two functions produce different assemblies, which tells me they're different. Can someone tell me in what way they are different? And is the function local static variable initialization in func2 thread-safe or not? If the answer depends on the compiler, I'd like to know how would the most common compilers behave with func2. int func1(int val) { const auto impl = [](int v) { return v * 10; }; return impl(val); } int func2(int val) { static const auto impl = [](int v) { return v

Why is the order of destruction of these function-local static objects NOT the inverse of their order of initialization?

时间秒杀一切 提交于 2019-12-04 22:14:50
问题 I have two function-local static objects, One and Two. One's constructor and destructor both access Two through GetTwo(): #include <iostream> struct One; struct Two; const One& GetOne(); const Two& GetTwo(); struct Two { const char* value = "It's two!"; Two() { std::cout << "Two construct" << std::endl; } ~Two() { std::cout << "Two destruct" << std::endl; } }; struct One { One() { std::cout << "One construct" << std::endl; const char* twoval = GetTwo().value; std::cout << "twoval is: " <<

Is initialization of local static function-object thread-safe?

五迷三道 提交于 2019-12-04 05:39:20
The following two functions produce different assemblies, which tells me they're different. Can someone tell me in what way they are different? And is the function local static variable initialization in func2 thread-safe or not? If the answer depends on the compiler, I'd like to know how would the most common compilers behave with func2. int func1(int val) { const auto impl = [](int v) { return v * 10; }; return impl(val); } int func2(int val) { static const auto impl = [](int v) { return v * 10; }; return impl(val); } "The most common compilers" probably differ on this, as they have not all

Initialize static std::map with non copyable value in a uniformed inline initialization

荒凉一梦 提交于 2019-12-04 04:38:38
I'd like to initialize a static std::map where the value is not copyable. I'll call my class ValueClass . ValueClass has an std::unique_ptr as private member and I even ensure that ValueClass is not copyable by extending non_copyable that looks like the following: class non_copyable { public: non_copyable() = default; protected: virtual ~non_copyable() = default; private: non_copyable(const non_copyable&) = delete; non_copyable& operator=(const non_copyable&) = delete; }; Now I'm trying to define a std::map using my class as value: static std::map<int, ValueClass> value_classes = { {0,

MSVC 2017 violating static initialization order within single translation unit

岁酱吖の 提交于 2019-12-04 04:03:33
问题 MSVC 2017 Community with -std=c++17 chokes on the following example: #include <iostream> struct TC { static TC const values[]; static TC const& A; static TC const& B; static TC const& C; int const _value; }; inline constexpr TC const TC::values[]{ { 42 }, { 43 }, { 44 } }; inline constexpr TC const& TC::A{ values[0U] }; inline constexpr TC const& TC::B{ values[1U] }; inline constexpr TC const& TC::C{ values[2U] }; int main(int, char**) noexcept { std::cout << std::boolalpha << "&A == &values

__attribute__((constructor)) call order confusion

断了今生、忘了曾经 提交于 2019-12-03 17:49:57
问题 The answer here demonstrates that __attribute__((constructor)) is not called after static initialization, it is called in the declaration order. Then, what is the purpose of it, if it is not guaranteed to be called when all data is initialized? We could as well have our ((constructor)) code in the Foo constructor. What I'm looking for is a way to have, in a shared library, a code that will be executed after all static data is initialized and static constructors are called. I saw people

Can “construct on first use” idiom fail under any circumstances?

随声附和 提交于 2019-12-03 16:04:55
I'm building my program (tests actually) using some static library. This library contains one file inside which I have functions like that: string& GetString() { static string strFilename; return strFilename; } void PrintToScreen() { printf("String: %s\n", GetString().c_str()) } Then in my main.cpp (outside the library) I'm doing: GetString() = "abc"; printf("String: %s\n", GetString().c_str()); PrintToScreen(); And I get this output: String: abc String: So looks like second call to the function (but done from different file, which is inside the library) somehow clear previous value,

Why is the order of destruction of these function-local static objects NOT the inverse of their order of initialization?

末鹿安然 提交于 2019-12-03 15:48:49
I have two function-local static objects, One and Two. One's constructor and destructor both access Two through GetTwo(): #include <iostream> struct One; struct Two; const One& GetOne(); const Two& GetTwo(); struct Two { const char* value = "It's two!"; Two() { std::cout << "Two construct" << std::endl; } ~Two() { std::cout << "Two destruct" << std::endl; } }; struct One { One() { std::cout << "One construct" << std::endl; const char* twoval = GetTwo().value; std::cout << "twoval is: " << twoval << std::endl; } ~One() { std::cout << "One destruct" << std::endl; const char* twoval = GetTwo()

Is what constitutes a failed initialization of block-scope static or thread storage duration variables underspecified?

亡梦爱人 提交于 2019-12-03 13:16:29
After answering this question and not finding a satisfying answer in the standard paper, I started wondering. The standard states the following w.r.t. initialization of mentioned variables: §6.7 [stmt.dcl] p4 [...] Otherwise such a variable is initialized the first time control passes through its declaration; such a variable is considered initialized upon the completion of its initialization . If the initialization exits by throwing an exception, the initialization is not complete, so it will be tried again the next time control enters the declaration. There is no mentioning of what might

non-deferred static member initialization for templates in gcc?

旧街凉风 提交于 2019-12-03 12:27:28
问题 Does gcc have any guarantees about static member initialization timing, especially regarding template classes? I want to know if I can get a hard guarantee that static members ( PWrap_T<T>::p_s ) will be initialized before main() , when classes are instantiated across multiple compilation units. It isn't practical to try to manually touch a symbol from each compilation unit at the start of main, but it isn't clear to me that anything else would work. I've tested with methods like bar() in