constants

Where to initialize static const member in c++17 or newer?

試著忘記壹切 提交于 2021-01-29 05:58:11
问题 The questions is where to initialize static const member in c++17 or newer? Please consider the following two solutions for initializing a static const member in c++: Solution 1 (for c++14 or older): //foo.h: #include <iostream> struct foo{ static const std::string ToBeInitialized; }; //foo.cpp #include <iostream> #include "foo.h" const std::string foo::ToBeInitialized{"with a value"}; Solution 2 (for c++17 or newer): //foo.h: #include <iostream> struct foo{ inline static const std::string

How to skip a lot of values when define const variable with iota?

六眼飞鱼酱① 提交于 2021-01-29 04:03:32
问题 Say I have next c program: #include <stdio.h> int main(int args, char* argv[]) { enum RC { APPLE=0, ORANGE, PEAR, BANANA=99, GRAPE }; printf("%d, %d, %d, %d, %d\n", APPLE, ORANGE, PEAR, BANANA, GRAPE); } The output is: 0, 1, 2, 99, 100 If in go, how can I use a more golang way to handle that? In fact, if I just want to skip some value. e.g. print 0, 1, 2, 5, 6 , then I can use next to skip some value, but here I need to skip 96 values... package main import "fmt" func main() { const ( APPLE =

Assigning const int to a const pointer to int is illegal?

↘锁芯ラ 提交于 2021-01-28 11:24:07
问题 Why is the following illegal? extern const int size = 1024; int * const ptr = &size; Surely a pointer to non-const data should be allowed to point to a const int (just not the other way around)? This is from C++ Gotchas item #18 回答1: If you really meant one of const int * const ptr = &size; const int * ptr = &size; that is legal. Yours is illegal. Because it it wasn't you could do int * ptr const = &size; *ptr = 42; and bah, your const was just changed. Let's see the other way around: int i =

How to initialize a constant int to use for array size?

谁说我不能喝 提交于 2021-01-28 10:23:37
问题 I have a static integer variable Game::numPlayers , which is read in as an input from user. I then have the following class defined as such: class GridNode { private: /* Members */ static const int nPlayers = Game::numPlayers; std::vector<Unit> units[nPlayers]; //... }; This won't compile ("in-class initializer for static data member is not a constant expression"). I obviously cant just assign the array size of Game::numPlayers , and I also tried not initializing it and letting a constructor

Does Flash pre-define any keyCode constants?

∥☆過路亽.° 提交于 2021-01-28 06:41:40
问题 I would have expected, given the way Adobe seems to do things, that you could reference some of the non-ASCII keycodes using a static constant, for example KeyCode.UP_KEY . Am I dreaming? Or do you just assume 38 will be the up key in perpetuity..? 回答1: I believe you are looking for the Keyboard class Keyboard.UP will refer to the up key, Keyboard.DOWN to the down key, and so on. See http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/ui/Keyboard.html for more key codes.

Union of const/non-const Object Pointers

扶醉桌前 提交于 2021-01-27 22:08:55
问题 Consider the sample code below: class A { public: A() : n(0) {} int n; }; class B { public: B(A* a) : mA(a) { } B(const A* a) : mConstA(a) { } union { A* mA; const A* mConstA; }; }; int main() { A a; B b1(&a); B b2(const_cast<const A*>(&a)); return 0; } At construction, b1 would have a mutable pointer to a , while b2 would have an immutable pointer to a . In this scenario, b1.mA equals b2.mConstA , and b1.mConstA equals b2.mA . Are these equalities always true when you have a union of const

Union of const/non-const Object Pointers

冷暖自知 提交于 2021-01-27 20:02:36
问题 Consider the sample code below: class A { public: A() : n(0) {} int n; }; class B { public: B(A* a) : mA(a) { } B(const A* a) : mConstA(a) { } union { A* mA; const A* mConstA; }; }; int main() { A a; B b1(&a); B b2(const_cast<const A*>(&a)); return 0; } At construction, b1 would have a mutable pointer to a , while b2 would have an immutable pointer to a . In this scenario, b1.mA equals b2.mConstA , and b1.mConstA equals b2.mA . Are these equalities always true when you have a union of const

Is it legal C to declare a non-const variable 'const' externally?

时间秒杀一切 提交于 2021-01-27 14:26:44
问题 Say I have a file, window.h, which defines: extern const int window_width, window_height; I don't want anyone to change these variables, so they're const for all includers of window.h. However, is it legal to declare them non-const in the source file? // window.c int window_width, window_height; void onResizeWindow(int w, int h) { window_width = w; window_height = h; } This compiles without linker errors for me in Apple clang version 12.0.0 (clang-1200.0.22.7). But is it legal and well

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

Using 'const' in C, what porting trouble might that cause?

☆樱花仙子☆ 提交于 2021-01-27 06:47:30
问题 I would like to use 'const' in C interface functions to note that certain char * arguments are not modified by the function. What trouble might this cause in porting this code to various platforms? Is support of 'const' in C code pretty standard? When did this become officially in C standard? 回答1: I can't imagine const not being supported by any compilers, so porting should be a non-issue. If you were to find such a beast, you could just put #define const Somewhere in a common header file to