Questions tagged [inline-variable]
21 questions
25
votes
3 answers
Are inline variables unique across boundaries?
This is a follow up of this question.
As mentioned in the comments to the answer:
An inline variable has the property that - It has the same address in every translation unit. [...] Usually you achieved that by defining the variable in a cpp file,…

skypjack
- 49,335
- 19
- 95
- 187
22
votes
2 answers
Why does cppreference define type_traits xxx_v shortcuts as inline constexpr and not just constexpr?
Why does cppreference define type_traits xxx_v shortcuts as inline constexpr and not just constexpr?
For example, see is_integral_v:
template< class T >
inline constexpr bool is_integral_v = is_integral::value;
Is this just a matter of style or…

Danra
- 9,546
- 5
- 59
- 117
12
votes
1 answer
Are all constexpr variable implicitly inline?
I was playing around with auto template parameters and I was surprised that this code didn't compiled:
constexpr auto bar = 2;
template
struct Foo {
auto operator()() const { return T; }
};
int main() {
Foo b;
…

Guillaume Racicot
- 39,621
- 9
- 77
- 141
9
votes
1 answer
Static initialization order of inline variables in single TU
I'm aware this question has been asked many times, but this seems to be a slightly different variation which I can't figure out.
Consider the following code:
#include
struct TestValue;
inline const TestValue* v_ptr = nullptr;
struct…

tchatow
- 748
- 1
- 7
- 16
6
votes
1 answer
Why isn't inv_sqrt2 defined in the C++ standard library?
C++20 introduces standard library header, , with definitions in namespace std::numbers for math constants such as sqrt2 and sqrt3. It provide inverse values like inv_sqrt3, but not inv_sqrt2. Why is inv_sqrt2 missing?

John McFarlane
- 5,528
- 4
- 34
- 38
6
votes
1 answer
When are inline variables in static storage initialized?
C++ standards (earlier than C++17, at least) have said this about initialization order.
Objects with static storage duration defined in namespace scope in the same translation unit and dynamically initialized shall be initialized in the order in…

Drew Dormann
- 59,987
- 13
- 123
- 180
5
votes
1 answer
Initializer "sizeof(T)" of inline static auto... Does it need instantiation?
What should happen if an expression's type is not dependent, but we use it to initialize a static auto variable? GCC and Clang differ in their behavior
template
struct A {
static inline auto x = sizeof(T{}.f);
};
A a;
GCC…

Johannes Schaub - litb
- 496,577
- 130
- 894
- 1,212
4
votes
1 answer
Multiple destruction of an inline variable
Here is a header file containing an inline variable:
// inline.hpp
#pragma once
#include
struct Test {
~Test() { std::cout << "deleted" << std::endl; }
};
inline const Test test;
...included into two .cpp files:
//…

slyx
- 2,063
- 1
- 19
- 28
4
votes
2 answers
Inline std::mutex in header file
I am using a global std::mutex in different cpp files.
Is it okay to declare it in a header file as inline?
inline std::mutex mtx;
Is mtx constructed this way?
Should it be initialized explicitly? As in:
inline std::mutex mtx = {};

Loreto
- 674
- 6
- 20
3
votes
1 answer
Initialization order of inline variables
Assume I have the following three files.
header.h:
int SomeFunction();
inline int a = SomeFunction();
file1.cpp:
#include "header.h"
int b = a + 5;
file2.cpp
#include "header.h"
int c = a + 3;
Am I guaranteed that a is initialized before both b…

dgnuff
- 3,195
- 2
- 18
- 32
3
votes
1 answer
Any potential pitfall of using inline static data member?
C++17 introduced inline variable, and an inline static data member can be defined in the class definition with an initializer. It does not need an out-of-class definition. For example,
struct X {
inline static int n = 1;
};
Given this, I see no…

Lingxi
- 14,579
- 2
- 37
- 93
2
votes
0 answers
Python: Multiline strings with inline variables
I'm trying to use inline variables in the multiline string to form a GraphQL query.
query = '''query {
gapc {
search_gapc_parts(part_number: "xxx", query: "XXX" limit: 1) {
items {
gapc_attributes {
name
value
…

Ali Guliyev
- 39
- 2
2
votes
1 answer
How to inline static array variables in Rio
I am failing to inline constructs such as
var FileName: array[0..2047] of Char;
This works:
procedure TForm1.AcceptFiles(var Msg: TWMDropFiles);
var FileName: array[0..2047] of Char;
begin
DragQueryFile(msg.Drop, $FFFFFFFF, FileName, 2048);
…

Gad D Lord
- 6,620
- 12
- 60
- 106
1
vote
1 answer
Inline declarations: var vs const
When I use inline declarations, should I prefer const over var?
In all online examples, and even in Delphi's own documentation, I see that var is being used. However, I think that const often better expresses my intentions, and prevents accidental…

Wouter van Nifterick
- 23,603
- 7
- 78
- 122
1
vote
1 answer
Initialization of inline static data
I am wondering how and when static inline data is initialized in C++ (for example in gcc or clang). I know it is a question specific to some architecture and it is not related to the C++ standard.
I know that for static/global non-inline data gcc is…

roy cabouly
- 487
- 4
- 12