static-members

Do c++ guarantee header-initialized static const member to share a single instance across compile units and libraries?

萝らか妹 提交于 2020-06-16 09:51:02
问题 Let's consider a code header: class uid { public: uid () {++i; } static int i; }; class foo { public: const static uid id; } source: static int uid::i = 0; The header could be included into several source files, shared between compiler units and libraries. Is it guaranteed that there would be only one instance off foo::id , that foo::id::id() would be called once at run-time and, the most important thing, would foo::id.i be the same everywhere in the program and it's libraries? On the other

g++ and clang++ different behaviour with recursive initialization of a static member

僤鯓⒐⒋嵵緔 提交于 2020-06-11 16:09:54
问题 Given the following code: #include <iostream> template <std::size_t N> struct foo { static std::size_t value; }; template <> std::size_t foo<0>::value = 0u; template <size_t N> std::size_t foo<N>::value = 1u + foo<N - 1u>::value; int main() { std::cout << foo<3u>::value << ' ' << foo<2u>::value << ' ' << foo<1u>::value << ' ' << foo<0u>::value << std::endl; } where the static member value of the template struct foo is recursively initialized, I get different outputs from g++: 3 2 1 0 and from

g++ and clang++ different behaviour with recursive initialization of a static member

最后都变了- 提交于 2020-06-11 16:06:08
问题 Given the following code: #include <iostream> template <std::size_t N> struct foo { static std::size_t value; }; template <> std::size_t foo<0>::value = 0u; template <size_t N> std::size_t foo<N>::value = 1u + foo<N - 1u>::value; int main() { std::cout << foo<3u>::value << ' ' << foo<2u>::value << ' ' << foo<1u>::value << ' ' << foo<0u>::value << std::endl; } where the static member value of the template struct foo is recursively initialized, I get different outputs from g++: 3 2 1 0 and from

Class template static data-member definition/declaration/initialization

核能气质少年 提交于 2020-03-24 00:50:26
问题 I know that the question has been asked several times and I've been reading posts like: Initializing static members of a templated class How can I Declare/define/initialize a static member variable of template classes as static member variables of a class? static member initialization for specialized template class However, I'm still struggling putting together all the pieces about templates, specializations, static data members definition and declarations. What I have is something like:

Static map initializer function error

[亡魂溺海] 提交于 2020-02-25 07:25:48
问题 I get the following base error: 1>c:\program files\microsoft visual studio 10.0\vc\include\utility(163): error C2436: 'second' : member function or nested class in constructor initializer list As well as a lot of sub-errors there - I have no idea at all where to look or what goes wrong. (I know what functions it is about, but I'm staring myself blind on why it doesn't work) The header part: typedef void *DuplicateFn(pTree&, const pTree&); enum DuplicateTy { SKIP, OVERWRITE, ASK }; typedef std

Why union static members not stored as a union?

痞子三分冷 提交于 2020-02-15 10:45:12
问题 In C++ union can contain static members which, as in the case of classes, belong to a class and therefore are common to all objects. union U { long l; int i; static long sl; static int si; }; int U::si; long U::sl; It would be logical to expect that all the union static members stored at the same address similar to non-static members storing. But it is not. A simple example shows that static members are stored under different addresses and can contain independent values. int main() { U u; u

Why union static members not stored as a union?

狂风中的少年 提交于 2020-02-15 10:44:26
问题 In C++ union can contain static members which, as in the case of classes, belong to a class and therefore are common to all objects. union U { long l; int i; static long sl; static int si; }; int U::si; long U::sl; It would be logical to expect that all the union static members stored at the same address similar to non-static members storing. But it is not. A simple example shows that static members are stored under different addresses and can contain independent values. int main() { U u; u

C++ static member of class template — linker warning “multiple definition” [duplicate]

a 夏天 提交于 2020-01-23 13:21:28
问题 This question already has answers here : Why can templates only be implemented in the header file? (16 answers) Static member initialization in a class template (3 answers) Closed 2 years ago . Let's say for some reason, I want to have a class template MyTemp with some static data member smDummyVar : Mytemp.h #ifndef MY_TEMP_H #define MY_TEMP_H template<class T> class MyTemp{ ... private: static int smDummyVar; ... }; #include "MyTemp.cpp" #endif //MY_TEMP_H Mytemp.cpp ... template<class T>

C++ static member of class template — linker warning “multiple definition” [duplicate]

懵懂的女人 提交于 2020-01-23 13:21:11
问题 This question already has answers here : Why can templates only be implemented in the header file? (16 answers) Static member initialization in a class template (3 answers) Closed 2 years ago . Let's say for some reason, I want to have a class template MyTemp with some static data member smDummyVar : Mytemp.h #ifndef MY_TEMP_H #define MY_TEMP_H template<class T> class MyTemp{ ... private: static int smDummyVar; ... }; #include "MyTemp.cpp" #endif //MY_TEMP_H Mytemp.cpp ... template<class T>

Why can we have static final members but cant have static method in an inner class?

╄→尐↘猪︶ㄣ 提交于 2020-01-21 04:45:11
问题 Why can we have static final members but cant have static method in an non static inner class ? Can we access static final member variables of inner class outside the outer class without instantiating inner class ? 回答1: YOU CAN have static method in a static "inner" class. public class Outer { static String world() { return "world!"; } static class Inner { static String helloWorld() { return "Hello " + Outer.world(); } } public static void main(String args[]) { System.out.println(Outer.Inner