Menu
  • HOME
  • TAGS

Class with private constructor and static array of itself

c++,c++11,initialization,static-initialization,stdarray

The solution: std::array<SomeThing, 64> SomeThing::lookup_table_0 {{ }}; Note: as explained here, {{}} is required to value-initialize the std::array without warnings in gcc. = {} and {} are correct but gcc warns anyway. The key to the solution is that some form of initializer must be present. A terminology check first:...

How can one accomplish this static initialization in ANSI C? Macro?

c,static,static-initialization

You can write as follows in C99. sample #include <stdio.h> #include <stdlib.h> #include <stdint.h> typedef struct ex { uint16_t m1; int m2; char *m3; } Type; int main(void){ Type *p = calloc(1, sizeof(Type)); *p = (Type){ .m1 = 4 | 2, .m3 = "string" //can't put ';' }; printf("m1:%hu\nm2:%d\nm3:%s\n", p->m1,...

Are there circumstances under which Java does not initialize static fields immediately?

java,static-initialization

Static variables are instantiated by the JVM classloader and are shared by every instance of the class. public class StaticVars { static int i = 3; public static void main( String[] args ) { System.out.println( "Called at Runtime: " + getStaticVar() ); System.out.println( "Called via it's static member: " +...

Fortran derived type in common: initialization?

fortran,fortran90,static-initialization

From the line integer :: acc = -1 strip off the trailing = -1 to leave integer :: acc recompile, and see what happens. The error message suggests that a program can't initialise a derived type component and use variables of that derived type in common statements. 'Initialize' is used...

Java: use static initializer blocks to register classes to global static registry

java,static-initialization

I read that the static initializers are executed when the class is first loaded and since I do not reference those classes anywhere in code directly, they are not loaded and the static initializers are not executed. This is correct -- unless you access the class somewhere, none of...

Initializing a static std::map> in C++

c++,templates,inheritance,static-initialization

This is one possible implementation: template <typename T, typename U> class create_map { private: std::map<T, U> m_map; public: create_map(T key, U val) { m_map.emplace(std::move(key), std::move(val)); } create_map&& operator()(T key, U val) && { m_map.emplace(std::move(key), std::move(val)); return std::move(*this); } operator std::map<T, U>() && { return std::move(m_map); } }; Note the taking...