8,944 questions
0
votes
0
answers
28
views
What are examples of non-finite float values other than infinity and NaN?
C23 §5.2.5.3.3 [Characteristics of floating types <float.h>] paragraph 8 says:
Floating types shall be able to represent signed zeros or an unsigned zero and all normalized floating-point ...
2
votes
1
answer
72
views
Is it legal to declare a function template of type which has C language linkage?
Function templates are not allowed to have C language linkage:
extern "C" {
template<typename T> // error: template with C linkage
void bad_f() {}
}
This is reasonable, as ...
5
votes
1
answer
101
views
What's the difference between `extern "C" /*...*/` and `extern "C" { /*...*/ }`
Both GCC and Clang reject
extern "C" static void foo() {}
but accept
extern "C" {
static void foo() {}
}
Aren't these supposed to be equivalent?
8
votes
1
answer
637
views
When is reinterpret_cast UB?
Does the code below contain undefined behavior (UB)?
struct A
{
int x;
char y;
};
int main()
{
std::vector<uint8_t> v(sizeof(A), 0);
A* p = reinterpret_cast<A*>(v.data());...
-2
votes
2
answers
224
views
Where in their documents do implementations state they won't reorder black-box functions?
Consider this example:
extern void black_box_foo();
extern void black_box_bar();
int main(){
black_box_foo(); // #1
black_box_bar(); // #2
}
#1 and #2 are functions whose definitions are ...
10
votes
3
answers
1k
views
Is creating an object not the same thing as starting its lifetime?
I've always assumed that "creating an object" is the same thing as "starting its lifetime" (and not the same thing as allocating storage for it).
But I've recently been told that &...
3
votes
1
answer
139
views
Do methods on the ECMAScript Atomics object enforce that all prior shared memory operations are completed first?
The ECMAScript Language Specification states:
Atomics are carved in stone: Program transformations must not cause any Shared Data Block events whose [[Order]] is seq-cst to be removed from the is-...
17
votes
3
answers
874
views
Necessity of `typename` for naming of local nested classes in function templates
I have a function template f, defining in its body a local class A with another nested class B. Both classes are not templates. Must I name the inner class as typename A::B or shorter variant A::B is ...
0
votes
1
answer
315
views
Can external IO operations be considered as if seq_cst operations in the reasoning of multithreaded programs?
Consider this example:
// thread A:
start_transaction();
update_mysql();
commit_transaction(); // remove "key" from mysql tables
remove_redis_cache("key");
// thread B:
std::...
9
votes
1
answer
288
views
How can you prevent a destructor call from being reordered above an atomic write by the compiler?
Here is a possibly incorrect program for communicating between a function and a signal handler that might interrupt it. Assume that HandleSignal has been arranged to run as a signal handler. The ...
14
votes
1
answer
857
views
Are out-of-bounds usize slice indexes guaranteed to panic?
Rust programs typically panic when you index into a slice with an out-of-bounds usize index.
Is that panic actually guaranteed to happen? Can I rely on the panic when reasoning about the soundness of (...
14
votes
1
answer
357
views
Are pointers to pure virtual member functions of local classes allowed?
Consider the following snippet:
int main() {
struct Local {
virtual void foo() = 0;
};
void (Local::* ptr)() = &Local::foo;
}
When compiling with C++20, GCC 13.3.0 and Clang 18.1.3 ...
4
votes
1
answer
220
views
Is it legal to cast a repr(C) struct pointer to pointer to its first field?
In C, this is legal as far as I know (In C, does a pointer to a structure always point to its first member?).
#include <stdio.h>
typedef struct {
char *name;
int age;
} A;
typedef ...
0
votes
1
answer
162
views
Does an implementation that reorders evaluation in a single thread violate [intro.execution] p8?
[intro.execution] p8 says:
Given any two evaluations A and B, if A is sequenced before B (or, equivalently, B is sequenced after A), then the execution of A shall precede the execution of B.
...
0
votes
1
answer
149
views
Does this approach to examine object representation reuse storage?
In order to examine object representation, the C++ standard provides https://timsong-cpp.github.io/cppwp/n4861/basic.lval#11:
If a program attempts to access ([defns.access]) the stored value of an ...
0
votes
1
answer
174
views
Is this a conforming observable behavior in the abstract machine's sense, where the load reads a value that is not currently produced
Consider this example:
#include <atomic>
#include <iostream>
#include <chrono>
#include <thread>
#include <cassert>
int main(){
std::atomic<int> val = {0};
...
10
votes
1
answer
281
views
Why does the memory order need to be Acquire in a single consumer linked-list queue when comparing pointer values?
This is a multi-producer single-consumer implementation translated from Rust, for the language-lawyer question, rewriting it in C++
template<class T>
struct Node{
std::atomic<Node*> ...
3
votes
1
answer
244
views
Need help understanding Synthesized three-way comparison
Can someone help me understand this part of cppreference?
https://en.cppreference.com/w/cpp/language/default_comparisons.html
Synthesized three-way comparison
The synthesized three-way comparison of ...
7
votes
1
answer
331
views
<stdalign.h> and C++
C11 introduced <stdalign.h>, which defined the macros alignas to _Alignas and alignof to _Alignof. Additionally, the feature test macros __alignas_is_defined and __alignof_is_defined are defined ...
0
votes
0
answers
114
views
Can std::(weakly)canonical return an error for existing directory absolute path?
Based on cppreference, I was under impression that std::canonical and std::weakly_canonical return the input path if it is an absolute directory path to existing directory that has no dot, dot-dot ...
15
votes
1
answer
1k
views
Is it Undefined Behavior to backport namespace std features to older C++ versions?
According to What are the reasons that extending the std namespace is considered undefined behavior?, adding anything to namespace std is Undefined Behavior, with some exceptions carved out, such as ...
6
votes
1
answer
165
views
Is pointer arithmetic on a pointer that points to a destroyed array element well-defined?
In Cppreference the rules for pointer arithmetic include:
If P points to the i-th element of an array object x with n elements, given the value of J as j, P is added or subtracted as follows:
– P + J ...
14
votes
2
answers
318
views
Is clang broken with `-std=c++20` and `-Wdtor-name`
Consider this simple code.
struct Foo {
template <typename T>
struct Bar {
~Bar();
};
};
with the out-of-line implementation
template <typename T>
Foo::Bar<T>::~...
11
votes
1
answer
277
views
typename in qualified member access
I wonder whether this piece of code should compile and what function it should call. The situation is that there is a struct whose name matches that of a template parameter. My question is, inside the ...
3
votes
1
answer
214
views
Use decltype(auto) return type to sometimes return references
I have the following code to normalize std::filesystem::path to always use forward slashes as separators:
static decltype(auto) fix_path_separator(const std::filesystem::path& path) {
if ...