static-members

constexpr initializing static member using static function

你说的曾经没有我的故事 提交于 2019-11-27 02:07:33
Requirements I want a constexpr value (i.e. a compile-time constant) computed from a constexpr function. And I want both of these scoped to the namespace of a class, i.e. a static method and a static member of the class. First attempt I first wrote this the (to me) obvious way: class C1 { constexpr static int foo(int x) { return x + 1; } constexpr static int bar = foo(sizeof(int)); }; g++-4.5.3 -std=gnu++0x says to that: error: ‘static int C1::foo(int)’ cannot appear in a constant-expression error: a function call cannot appear in a constant-expression g++-4.6.3 -std=gnu++0x complains: error:

How to have static data members in a header-only library?

*爱你&永不变心* 提交于 2019-11-27 01:55:24
What is the best way to have a static member in a non-templated library class, without placing the burden of defining the member on the class user? Say I want to provide this class: class i_want_a_static_member { static expensive_resource static_resource_; public: void foo() { static_resource_.bar(); } }; Then the user of the class must not forget to define the static member somewhere (as already answered many times ): // this must be done somewhere in a translation unit expensive_resource i_want_a_static_member::static_resource_; I do have an answer below, but it has some disadvantages. Are

Static member functions

…衆ロ難τιáo~ 提交于 2019-11-27 01:26:46
问题 After reading sbi and Eli Bendersky's answers in this question I started to wondering what static member functions are for. A class' friend free function shouldn't be able to do anything a static member function can do? If so, why/when should I prefer a static member function to a friend free one? 回答1: In general: Require access to private members static member functions have access to private members of the class. If you need that, you can use a static member function. You have to declare it

How to force a static member to be initialized?

别等时光非礼了梦想. 提交于 2019-11-27 01:02:02
Consider this example code: template<class D> char register_(){ return D::get_dummy(); // static function } template<class D> struct Foo{ static char const dummy; }; template<class D> char const Foo<D>::dummy = register_<D>(); struct Bar : Foo<Bar> { static char const get_dummy() { return 42; } }; ( Also on Ideone .) I'd expect dummy to get initialized as soon as there is a concrete instantiation of Foo , which I have with Bar . This question (and the standard quote at the end) explained pretty clear, why that's not happening. [...] in particular, the initialization (and any associated side

static vs extern “C”/“C++”

房东的猫 提交于 2019-11-27 00:58:20
What is the difference between a static member function and an extern "C" linkage function ? For instance, when using "makecontext" in C++, I need to pass a pointer to function. Google recommends using extern "C" linkage for it, because "makecontext" is C. But I found out that using static works as well. Am I just lucky or... class X { public: static void proxy(int i) {} } makecontext(..., (void (*)(void)) X::proxy, ...); vs extern "C" void proxy(int i) {} makecontext(..., (void (*)(void)) proxy, ...); EDIT: Can you show a compiler or architecture where the static member version does not work

When to use enums, and when to replace them with a class with static members?

断了今生、忘了曾经 提交于 2019-11-27 00:38:34
It recently occured to me that the following (sample) enumeration... enum Color { Red, Green, Yellow, Blue } ... could be replaced with a seemingly more type-safe class: class Color { private Color() { } public static readonly Color Red = new Color(); public static readonly Color Green = new Color(); public static readonly Color Yellow = new Color(); public static readonly Color Blue = new Color(); } With "type-safe", I mean that the following statement would work if Color was an enum, but not if Color were the above class: var nonsenseColor = (Color)17; // works if Color is an enum Two

Static properties in Swift

為{幸葍}努か 提交于 2019-11-27 00:14:14
I'm trying to convert the following Objective-C code to Swift. In my Objective-C code, there's a static variable and its accessed from a class method. @implementation SomeClass static NSMutableArray *_items; + (void)someMethod { [_items removeAll]; } @end Since you can't access types declared like this private var items = [AnyObject]() from class functions in Swift, I created a stored property for it like this. class var items: [AnyObject] { return [AnyObject]() } And I'm trying to call a method on it from a class function like so. class func someFunction() { items.removeAll(keepCapacity:

Which is best way to define constants in android, either static class, interface or xml resource?

醉酒当歌 提交于 2019-11-27 00:01:13
问题 I'm developing an android application which uses web service to get data from server, for that I'm having three different set of URLs to point development system, test server and live server. It's difficult to change URL whenever I want to give application for testing/live. so I planned to make it as configurable, so that application can get appropriate URL based on me build type configuration constant. So, which is the best way to keep this constants, java static class or java public

Why does constexpr static member (of type class) require a definition?

删除回忆录丶 提交于 2019-11-26 23:03:52
问题 ==> See the full snippet code and compilation on coliru. I have a LiteralType class filling constexpr requirements: struct MyString { constexpr MyString(char const* p, int s) : ptr(p), sz(s) {} constexpr char const* data() const { return ptr; } constexpr int size() const { return sz; } char const *ptr = 0; int const sz = 0; }; I use it as a constexpr static member variable: struct Foo { int size() { return str_.size(); } constexpr static MyString str_{"ABC",3}; }; int main() { Foo foo; return

c++ access static members using null pointer

亡梦爱人 提交于 2019-11-26 20:42:54
Recently tried the following program and it compiles, runs fine and produces expected output instead of any runtime error. #include <iostream> class demo { public: static void fun() { std::cout<<"fun() is called\n"; } static int a; }; int demo::a=9; int main() { demo* d=nullptr; d->fun(); std::cout<<d->a; return 0; } If an uninitialized pointer is used to access class and/or struct members behaviour is undefined, but why it is allowed to access static members using null pointers also. Is there any harm in my program? TL;DR : Your example is well-defined. Merely dereferencing a null pointer is