Linked Questions
12 questions linked to/from Define constant variables in C++ header
243 votes
3 answers
152k views
How do inline variables work?
At the 2016 Oulu ISO C++ Standards meeting, a proposal called Inline Variables was voted into C++17 by the standards committee. In layman's terms, what are inline variables, how do they work and what ...
100 votes
7 answers
25k views
Why does const imply internal linkage in C++, when it doesn't in C?
See subject. What were they thinking? UPDATE: Changed from "static" to "internal linkage" to save confusion. To give an example... Putting the following in a file: const int var_a = 1; int var_b = ...
99 votes
7 answers
39k views
How to declare constexpr extern?
Is it possible to declare a variable extern constexpr and define it in another file? I tried it but the compiler gives error: Declaration of constexpr variable 'i' is not a definition in .h: ...
68 votes
4 answers
47k views
static vs non-static variables in namespace
I have a namespace foo which contains an integer bar, declared so... foo.h: namespace foo { int bar; } Now if I include foo.h in only one file, this works just fine. But a problem arises when I ...
45 votes
4 answers
58k views
shared c constants in a header
I want to share certain C string constants across multiple c files. The constants span multiple lines for readability: const char *QUERY = "SELECT a,b,c " "FROM table..."; Doing ...
22 votes
3 answers
16k views
What effect does static const have on a namespace member
// MyClass.h namespace MyNamespace { static const double GasConstant = 1.987; class MyClass { // constructors, methods, etc. }; } I previously had GasConstant declared within the ...
6 votes
5 answers
3k views
c++ extern constant int for array size
I have the following three files in my code (with most of the code removed. This is just to isolate the issue). global.h: //global.h #ifndef GLOBAL_H #define GLOBAL_H extern const int ARRAYSIZEX; ...
0 votes
2 answers
8k views
How can I clear a certain number of bits in C?
I know there is another post here that is how to clear a single bit, but how about a whole byte? For example, if I had 00000000 00000000 00101100 11000100 and I wanted to clear the second chunk so it ...
2 votes
1 answer
2k views
extern "C" with const
We have a piece of C-code in our system which contains a few arrays like this with global access: source.h extern const int magic[5]; source.c: const int magic[] = { 1, 2, 3, 4, 5}; Somebody ...
1 vote
2 answers
585 views
Declare an array in header that used global variable as their elements?
I try to use a global variable as elements for an array. The problem is that the compiler want to know the integer constant value while declaring an array. I thought that my global variable is ...
2 votes
1 answer
518 views
Define replacement with Constant variable issue
I am creating an component, generalization of a template. For creation must be used string identifier. I am replacing: #define MYCOMPONENT_CONSTANT_IDENTIFIER "ID value" with namespace ...
1 vote
3 answers
351 views
I have a string which is used across different files and translation unit, should I use constexpr const char* or const std::string?
If I have a string variable in a header file which will be used across different files and translation units, should I store it in a constexpr const char* or const std::string? Here is an example: // ...