static-initialization


Initializing a static std::map<int, unique_ptr<int>> in C++

对着背影说爱祢 提交于 2020-01-03 17:06:05
问题 This is a similiar question to this post. The answer that I think has the most promise has to do with templated static initialization. Here is the class from that answer: template <typename T, typename U> class create_map { private: std::map<T, U> m_map; public: create_map(const T& key, const U& val) { m_map[key] = val; } create_map<T, U>& operator()(const T& key, const U& val) { m_map[key] = val; return *this; } operator std::map<T, U>() { return m_map; } }; Usage: std::map mymap = create

How Do Zero-Initialization, Static-Initialization, and Value-Initialization Differ?

半腔热情 提交于 2020-01-03 10:47:11
问题 Ben Voigt has pointed out here that: Zero initialization is one of the steps of static initialization. But you're right that you can't blindly substitute the latter (tag), since zero initialization is also performed for value initialization. However, there's no need for (a tag named) zero-initialization in the context of C++, because tags already exist for both static initialization and value initialization, and those are more relevant. I thought there was a case where it made sense to "Zero

Is it bad practice to specify an array size using a variable instead of `#define` in C++? (C error: variably modified at file scope) [closed]

和自甴很熟 提交于 2019-12-30 14:47:06
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed last year . In C, declaring an array size using a variable, even if it is a const variable, is NOT allowed. Ex: this fails to compile in C: #include <stdio.h> const int SIZE = 2; int a[SIZE]; int main() { a[0] = 1; a[1] = 2; printf("%i, %i", a[0], a[1]); return 0; } Run this code in C.

Static pthreads mutex initialization

别等时光非礼了梦想. 提交于 2019-12-30 02:44:33
问题 Using pthreads, how would one, in C, initialize a static array of mutexes? For a single static mutex, it seems I can use PTHREAD_MUTEX_INITIALIZER. But what about an static array of them? As, in for example, #include &ltpthread.h&gt #define NUM_THREADS 5 /*initialize static mutex array*/ static pthread_mutex_t mutexes[NUM_THREADS] = ...? Or must they be allocated dynamically? 回答1: If you have a C99 conforming compiler you can use P99 to do your initialization: static pthread_mutex_t mutexes

Default value for struct parameter

橙三吉。 提交于 2019-12-23 11:58:03
问题 Let's say I have the following struct: struct myStruct { int x; int y; int z; int w; }; I want to initialize this struct to a default value when calling the following function. If it helps I'm looking for a simple zero initialization. void myFunc(myStruct param={0,0,0,0}) { ... } This code however gives me compile error. I've tried VS2003 and VS2008. NOTE: I have looked at other answers mentioning the use of constructor. However I want the user to see what values I'm using for initialization.

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

那年仲夏 提交于 2019-12-21 04:31:26
问题 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

Initialize-On-Demand idiom vs simple static initializer in Singleton implementation

可紊 提交于 2019-12-20 02:32:38
问题 Is the Initialize-On-Demand idiom really necessary when implementing a thread safe singleton using static initialization, or would a simple static declaration of the instance suffice? Simple declaration of instance as static field: class Singleton { private static Singleton instance=new Singleton(); private Singleton () {..} public static Singleton getInstance() { return instance; } } vs class Singleton { static class SingletonHolder { static final Singleton INSTANCE = new Singleton(); }

Thread-safety of static initializers in C#

丶灬走出姿态 提交于 2019-12-19 19:04:14
问题 Everyone says static initializers are thread-safe, but I'm worried about a particular detail. Let's say I have static class MyStaticClass { public static readonly object myField = MyOtherClass.GetNewObject(); } static class MyOtherClass { public static object GetNewObject() { /* arbitrary code that returns a new object */ } } Which of the following does C# guarantee, when MyStaticClass.myField is not yet initialized? If threads 1 and 2 try to access myField together (in that order),

Thread-safety of static initializers in C#

三世轮回 提交于 2019-12-19 19:03:58
问题 Everyone says static initializers are thread-safe, but I'm worried about a particular detail. Let's say I have static class MyStaticClass { public static readonly object myField = MyOtherClass.GetNewObject(); } static class MyOtherClass { public static object GetNewObject() { /* arbitrary code that returns a new object */ } } Which of the following does C# guarantee, when MyStaticClass.myField is not yet initialized? If threads 1 and 2 try to access myField together (in that order),

How to comprehend that an implementation is permitted to treat dynamic initialization of non-local variable as static initialization in some cases?

浪子不回头ぞ 提交于 2019-12-19 09:21:39
问题 In fact, the problem comes from the words in the standard draft N4582: [basic.start.static/3] An implementation is permitted to perform the initialization of a variable with static or thread storage duration as a static initialization even if such initialization is not required to be done statically, provided that — the dynamic version of the initialization does not change the value of any other object of static or thread storage duration prior to its initialization, and — the static version

工具导航Map