static-initialization

C++ Nifty Counter idiom; why?

前提是你 提交于 2021-02-05 13:40:21
问题 I recently came across the Nifty Counter Idiom. My understanding is that this is used to implement globals in the standard library like cout, cerr, etc. Since the experts have chosen it, I assume that it's a very strong technique. I'm trying to understand what the advantage is over using something more like a Meyer Singleton. For example, one could just have, in a header file: inline Stream& getStream() { static Stream s; return s; } static Stream& stream = getStream(); The advantage is you

c++ static initialization order fiasco

只谈情不闲聊 提交于 2021-01-27 11:21:20
问题 I'm currently learning C++, and I'm having some troubles. I've developped a program by using lots of #define , but I'd like to use static const instead (collision/type/scopes...). So, I now have something like: file1.hpp class A { public: static const std::string MY_CONST_VAR; }; file1.cpp const std::string A::MY_CONST_VAR = "some string"; file2.cpp static std::string arrayOfString[] = { A::MY_CONST_VAR, ... }; My code compiles with no warnings/errors (compiling with -W -Wall -Wextra -Werror

java static initialization with inheritance

▼魔方 西西 提交于 2020-01-21 06:13:29
问题 public class Main { public static void main(String[] args) { System.out.println(B.x); } } class A { public static String x = "x"; } class B extends A { static { System.out.print("Inside B."); } } Question: Why output will be: x . But not: Inside B.x 回答1: The reference to B.x issues the following bytecode: getstatic #3 <Field int B.x> According to Java Virtual Machine Spec The Java virtual machine instructions anewarray, checkcast, getfield, getstatic , instanceof, invokedynamic,

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(); }