static-initialization

A better way to initialize a static array member of a class in C++ ( const would be preferred though )

点点圈 提交于 2019-12-12 01:59:06
问题 I have a static array of pointers to functions as a member of a class. I need to initialize it, but it turns out this array is 64K items long, so it's impractical to initialize it with a static initializer like { x, y, z, ... } as it would clutter code. I have instead to initialize it by code, with several loops. The way I figured to do this is by initializing the static array in the constructor and setting a flag to it, so only the construction of the first instance of the class would fire

Calling void function before main()

谁说我不能喝 提交于 2019-12-11 04:38:56
问题 I wanted to know if it was possible to call a void function without using a temp variable. E.g. in the following code block... #include <iostream> void earlyInit() { std::cout << "The void before the world." << std::endl; } int g_foo = (earlyInit(), 0); int main( int argc, char* argv[] ) { std::cout << "Hello, world!" << std::endl; } ...I have no need for g_foo and would rather it not exist. Is there a way to call void functions without an intermediate temp variable? 回答1: I wanted to know if

How to force the linker to honor object file order?

我的梦境 提交于 2019-12-10 17:54:43
问题 I'm catching a Valgrind finding on an uninitialized read. I know exactly where its coming from - its an empty std::string declared in a cpp file with static storage class. The object file that has the std::string 's storage allocation is listed first in the static archive. # string of interest is located in a.o LIBOBJS := a.o b.o c.o ... x.o y.o z.o library.a: $(LIBOBJS) $(AR) $(ARFLAGS) $@ $(LIBOBJS) $(RANLIB) $@ In addition, I modified the link recipe to the following (I know it looks silly

C++ is it possible to delay initialization of constant static member?

戏子无情 提交于 2019-12-10 14:59:29
问题 I am using Qt but this is a generic C++ question. My case is simple, I have a class Constants which has a constant static member which I want it to be initialized after certain function calls are made. Constants.h #ifndef CONSTANTS_H #define CONSTANTS_H class Constants { public: static const char* const FILE_NAME; }; #endif // CONSTANTS_H Constants.cpp #include "constants.h" #include <QApplication> const char* const Constants::FILE_NAME = QApplication::applicationFilePath().toStdString().c

Static initializer cannot reference a field before it is defined

时光总嘲笑我的痴心妄想 提交于 2019-12-10 10:58:33
问题 I have the following code with the error commented public final class MyStaticClass { private MyStaticClass() {} static { a = new A(); b = new B(a); // Cannot access a field before it is defined } private static final A a; private static final B b; } I'm fairly new to using static initializers, but I have no idea why this will not compile. I've looked around a few of the posts on this topic, and have seen the order that initialisation runs, but this doesn't seem to violate the rule. By the

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

时光总嘲笑我的痴心妄想 提交于 2019-12-09 17:07:03
问题 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

Crash when destructors for static objects are executing

夙愿已清 提交于 2019-12-09 03:44:23
There's a subtle bug that wouldn't manifest predictably in a piece of our software. It happens when global destructors are executing. Often it's a "double-free" error, but I've seen other things as well: NULL-ptr dereference, ptr dereference to an address where nothing is allocated, misaligned access (because the pointer had a garbage value), issues related to a corrupt stack ... the list goes on. The cause of these mysterious & difficult to reproduce bugs: a subtle violation of the One Definition Rule. A little background ... Since I was a little tike this software was being linked using the

Crash when destructors for static objects are executing

自古美人都是妖i 提交于 2019-12-08 05:30:24
问题 There's a subtle bug that wouldn't manifest predictably in a piece of our software. It happens when global destructors are executing. Often it's a "double-free" error, but I've seen other things as well: NULL-ptr dereference, ptr dereference to an address where nothing is allocated, misaligned access (because the pointer had a garbage value), issues related to a corrupt stack ... the list goes on. 回答1: The cause of these mysterious & difficult to reproduce bugs: a subtle violation of the One

The classical C++ static initialization order fiasco revisited

非 Y 不嫁゛ 提交于 2019-12-07 10:11:00
问题 I have encountered a strange situation recently. Let's consider the following class (place in header.h ): #ifndef HEADER_H #define HEADER_H #include <set> template <class V, class T> class Class { public: typedef std::set<const Class<V, T>* > instances_list; explicit Class(const V& Value):m_value(Value) { s_instances.insert(this); } private: static instances_list s_instances; V m_value; }; template <typename V, typename T> typename Class<V,T>::instances_list Class<V,T>::s_instances; class

Static initializer cannot reference a field before it is defined

喜欢而已 提交于 2019-12-06 04:35:23
I have the following code with the error commented public final class MyStaticClass { private MyStaticClass() {} static { a = new A(); b = new B(a); // Cannot access a field before it is defined } private static final A a; private static final B b; } I'm fairly new to using static initializers, but I have no idea why this will not compile. I've looked around a few of the posts on this topic, and have seen the order that initialisation runs, but this doesn't seem to violate the rule. By the time b is being initialized, a should already have been. I have a work around, which would be to set up