Questions tagged [language-lawyer]

For questions about the intricacies of formal or authoritative specifications of programming languages.

Filter by
Sorted by
Tagged with
1 vote
2 answers
55 views

Is accessing a member of an unaligned union undefined behavior even if the member being accessed is sufficiently aligned?

In C bad things can happen if I try to access a type through a pointer that is not aligned: int x[2]; // Assuming CHAR_BIT == 8 && sizeof(int) == 4 *(int *)((char *)x+1) = 10; /* Undefined ...
user16217248's user avatar
  • 3,200
2 votes
0 answers
32 views

Using function arguments within the constant expression of a non-type template paramater

In what context can a function argument be part of the constant expression provided to a non-type template parameter? Is the expected behavior affected by use of an unevaluated operand in any sort of ...
303's user avatar
  • 2,607
3 votes
0 answers
59 views

How are the immediately-declared constraint-expressions of type-constraints combined?

If C is a concept, are the following constructs equivalent? template <C A, C B> template <typename A, typename B> requires C<A> && C<B> In other words, are all the ...
Jan Schultke's user avatar
  • 20.7k
5 votes
1 answer
144 views

Is the initialization order of global variables and static inline data members relative to each other guaranteed?

The code below is in the same translation unit and A::v is defined after x, why A::v is not initialized to "ok" ? #include <string> #include <iostream> std::string foo() { ...
konchy's user avatar
  • 657
13 votes
3 answers
2k views

Is it undefined behaviour to use pointer after allocated memory?

I have the following code: uint8_t buffer[16]; uint8_t data[16]; uint8_t buffer_length = 16; uint8_t data_length = 0; memcpy(buffer + buffer_length, data, data_length); memcpy should be a no-op, ...
vbezhenar's user avatar
  • 11.4k
1 vote
2 answers
132 views

Is the author's union-based implementation of an optional<bool> well-defined in P2641?

In P2641r4: Checking if a union alternative is active, the author provides an implementation of an optional<bool> as a motivating example and claims that this is well-formed. struct OptBool { ...
Jan Schultke's user avatar
  • 20.7k
1 vote
1 answer
87 views

Is the author's cast-based implementation of an optional<bool> well-defined in P2641?

In P2641r4: Checking if a union alternative is active, the author provides an implementation of an optional<bool> as a motivating example and claims that this is well-formed. struct OptBool { ...
Jan Schultke's user avatar
  • 20.7k
2 votes
1 answer
63 views

@Nullable annotation cannot be applied here

Eclipse shows this as a Java problem: Annotation types that do not specify explicit target element types cannot be applied here. This only happened when I upgraded from Eclipse 2022-09 to 2023-09. ...
Daniele Repici's user avatar
2 votes
1 answer
102 views

Is GCC right about this being a VLA?

This question is quite similar to "Are conformant array parameters VLAs?", with the only difference being that here I'm using the static keyword within the [ and ] of the array declaration (...
Tachi's user avatar
  • 519
3 votes
1 answer
106 views

Why does a better standard conversion sequence followed by a user-defined conversion not make an overload better?

This is a follow-up question to how to avoid implicit cast from `double` to `int`?. Say I have the following code: template <class T> struct wrapper { wrapper(const T&); }; void f(...
Jan Schultke's user avatar
  • 20.7k
1 vote
1 answer
116 views

Is the compiler required to diagnose violations of language syntax in templates?

There is an example in [temp.res.general] which confuses me: template<class T> class X { // ... void g(T t) { +; // may be diagnosed even if X​::​g is not instantiated } }...
Jan Schultke's user avatar
  • 20.7k
4 votes
1 answer
83 views

Difference between GCC and CLANG about using-directive scope in C++ modules

When I put an unexported "using namespace" directive in a C++20 module interface file, it also applies to the separate module implementation file under CLANG, but not under GCC. So, which ...
Morgana's user avatar
  • 240
7 votes
0 answers
133 views

Does undefined behaviour retroactively mean that earlier visible side-effects aren't guaranteed?

In C++, a compiler can assume that no UB will happen, affecting behaviour (even visible side-effects like I/O) in paths of execution that will encounter UB but haven't yet, if I understand the ...
Peter Cordes's user avatar
8 votes
1 answer
119 views

Use of template keyword before dependent template name

Considering the following code example, I would expect to have to use the template keyword here to guide the compiler to treat variable v as a template. However, MSVC rejects the use of the template ...
303's user avatar
  • 2,607
1 vote
1 answer
52 views

Are default argument conversions considered in overload resolution?

Consider the following code: void foo(int* = 0); // (1) void foo(int = 0); // (2) int main() { foo(0); // OK, calls (2) foo(); // error, ambiguous overload } Both GCC and clang act like ...
Jan Schultke's user avatar
  • 20.7k
10 votes
2 answers
697 views

Are C++ compilers actually compliant with zero-size array SFINAE rule?

About a year or two ago I read about SFINAE rules in C++. They state, in particular, The following type errors are SFINAE errors: ... attempting to create an array of void, array of reference, array ...
ivanovmp's user avatar
  • 103
5 votes
2 answers
89 views

when will there be && + && -> && with c++ reference collapse?

I have some question about understanding reference collapse, so I do experiments in the following code: template<typename T> void func(T&& t) {} int main() { // There is no ...
coin cheung's user avatar
  • 1,021
4 votes
2 answers
166 views

Where is the definition of the acquire operation in the C++ 20?

I am reading the std::memory_order on the cppreference.com and the C++ 20 specification. I can't find the definition of the terms, "acquire operation" and "release operation". The ...
relent95's user avatar
  • 3,806
7 votes
3 answers
112 views

Why doesn't function declaration as body of If statement result in an error in JavaScript?

In the MDN documentation for "Statements and declarations" it is mentioned: The terms "statement" and "declaration" have a precise meaning in the formal syntax of ...
Urooj's user avatar
  • 323
-1 votes
1 answer
103 views

is a c++ array of size N a distinct type [duplicate]

I thought that int a1[5]; and int a2[6]; were distinct types. But void foo(int a[5]){}; void foo(int a[6]){}; will not compile saying that they are duplicated definitions of foo (ie foo(int *a)) I ...
pm100's user avatar
  • 48.2k
-1 votes
2 answers
47 views

Can the operation of the user-implemented atomic by assemble establish a synchronous relationship?

Consider a user-implemented atomic object, and the following code is pseudo-code: struct SpinLock{ bool* ptr void lock(){ // mov eax, false // new_val = true while /* lock ...
xmh0511's user avatar
  • 7,052
1 vote
1 answer
92 views

Can the read operations in `compare_exchange_strong` in different two thread read the same value?

[atomics.types.operations] p23 says: bool compare_exchange_weak(T& expected, T desired, memory_order success, memory_order failure) noexcept; Effects: Retrieves the value in expected. It then ...
xmh0511's user avatar
  • 7,052
3 votes
3 answers
134 views

C++23: Is it valid to modify the elements of a `zip_view`?

I am trying to figure out under which circumstances a modifying access to an element of a C++23 range (resp. view) is allowed by the standard. For example, consider this rather elegant version of ...
Lukas Barth's user avatar
  • 2,764
4 votes
2 answers
223 views

How do the C and C++ standards tell you to deal with situations that are not covered by them?

It is well known that in each of the C and C++ standards there were "blind spots" that did not describe certain cases in well-formed programs. It is obvious that in non-formal descriptions ...
cher-nov's user avatar
0 votes
0 answers
43 views

Using a protected inner class as a base class of other class

In the section "11.9 Member access control" of the C++20 Standard there is provided an example (p.#7, example #2) of using a protected inner class of a class as a base class for a derived ...
Vlad from Moscow's user avatar
1 vote
0 answers
53 views

Can you bind a const T*&& to an xvalue of type T*?

Consider the following code: (https://godbolt.org/z/8W699x6q6) int* p; const int*&& r = static_cast<int*&&>(p); Clang happily compiles this, but r binds to a temporary object: p:...
Jan Schultke's user avatar
  • 20.7k
6 votes
1 answer
89 views

Temporary object creation for reference parameter and optimization changes

I have the following code #include <iostream> void foo(const int* const &i_p) { std::cout << &i_p << std::endl; } int main () { int i = 10; int* i_p = &i; ...
User 10482's user avatar
8 votes
0 answers
195 views

What is the return type of ternary operator applied to 'const char* const&' and 'char* &'?

Consider the following useless code snippet, which gives inconsistent results with different compilers (demo) #include <type_traits> char* dummy; const char* const& a = dummy; char* ...
康桓瑋's user avatar
  • 34.3k
1 vote
2 answers
108 views

What is the name of the initialization used in char a[] = ""?

What type of initialization is the following? char a[] = ""; // ??? (copies array content) char* q = a; // copy-initialization (copies pointer) // EDIT: originally it was `...
OwnageIsMagic's user avatar
1 vote
3 answers
60 views

downcasting standard layout struct to derived struct with the same data members

I receive a pointer p to a struct S from C code and want to downcast it in C++ with static_cast<S_extended*>(p) where S_extended is derived from S but just adds some methods to S so that the ...
user396672's user avatar
  • 3,126
3 votes
3 answers
76 views

Use of an overloaded operator in a scoped enum definition

Given this code #include <iostream> #include <format> enum class Enum; constexpr Enum operator~(Enum) { return static_cast<Enum>(5); } enum class Enum { A = 1, B = ~Enum::A }; // ...
Kane's user avatar
  • 5,675
0 votes
1 answer
70 views

Why does pattern matching with instanceof fail to compile when using the boolean logical operator &?

When using instanceof as a pattern match operator in an if-statement, the code fails to compile if we use the boolean logical operator &, but succeeds if using &&. This DOES compile: ...
Lilypad's user avatar
  • 129
14 votes
1 answer
927 views

Is it really well defined to check pointer alignment using the pointer's integer value?

Is there a guaranteed (not implementation-defined!) way to check for pointer alignment? The most common way to query pointer alignment seems to be: convert to integer check whether the integer is a ...
Kietz's user avatar
  • 1,336
-2 votes
1 answer
77 views

Strict aliasing rule and copy kernel in cuda

Background I want to copy many arrays (they may have different types), like cudaMemcpy(dst0, src0, num0 * sizeof(int), cudaMemcpyDeviceToDevice); cudaMemcpy(dst1, src1, num1 * sizeof(float), ...
courage's user avatar
  • 85
7 votes
2 answers
272 views

All programs were UB before C++20?

Yesterday I heard this in a talk from David Stone Prior to C++20 it was not possible to implement std::vector, all vector implementations, if they are written in C++, had undefined behavior. But ...
Enlico's user avatar
  • 23.6k
2 votes
2 answers
84 views

Why are exceptions thrown during deferred dynamic initialization not caught by main?

The wording which confuses me is this: It is implementation-defined whether the dynamic initialization of a non-block non-inline variable with static storage duration is sequenced before the first ...
Jan Schultke's user avatar
  • 20.7k
2 votes
1 answer
71 views

uncaught exception in destructors of static objects

When I'm trying the following code: #include <thread> struct foo { ~foo() noexcept(false) { throw 0; } }; void func() try { static foo f; } catch(...) { } int main() { std::...
Mia 's user avatar
  • 43
2 votes
1 answer
89 views

C array declarators

The C standard link states in Section 6.7.5 Declarators the following syntax: declarator: pointeropt direct-declarator direct-declarator: identifier ( declarator ) direct-declarator [ ...
Bogdan Tanasa's user avatar
4 votes
1 answer
129 views

Are these expressions involving compound literals defined in C?

Are these expressions defined? int x = 5, y; y = ++(int){++x}; //or y = (int){x++}++; and (I cant find any reason why not to be defined) int x = 5; x = ++(int){++x}; //or ...
0___________'s user avatar
1 vote
4 answers
103 views

C unary operators

I try to understand the grammar behind the unary operators in the C language. According to this version of the standard link, in Section 6.5.3 Unary operators have the following syntax: unary-...
Bogdan Tanasa's user avatar
-1 votes
0 answers
40 views

GCC 4.8 error when returning tuple constructed from initializer list [duplicate]

We are still using old RHEL7 server with gcc 4.8 for our Jenkins CI, and I recently noticed that the following code fails to compile: #include <tuple> std::tuple<int, int> foo() { int ...
pptaszni's user avatar
  • 5,831
3 votes
1 answer
94 views

Why can't I specialize a template with an alias template in the return type in MSVC?

MSVC refuses to compile this: #include <type_traits> struct G { void operator()() const { } }; template<class T> using MyTrait = std::is_void<T>; struct S { template<class ...
user541686's user avatar
  • 205k
2 votes
1 answer
114 views

Illegal use of undefined type

Considering the code example shown below, is decltype(h{}.t()); a legal expression? Which specific rule in the C++20 standard either allows or forbids this? struct d; struct h { static auto t() -> ...
303's user avatar
  • 2,607
1 vote
0 answers
34 views

error: reference to local binding 'x' declared in enclosing function [duplicate]

A similar code has failed to compile in our pipeline (Clang 15.0.2), but I have managed to reproduce the error. https://godbolt.org/z/o4cfveWY8 #include <tuple> #include <iostream> int ...
Sergey Kolesnik's user avatar
6 votes
1 answer
124 views

Does placement-new of array of bytes (which implictly creates objects) end the lifetime of the object that previously occupied that storage?

P0593, under the Type punning section, presents this example: float do_bad_things(int n) { alignof(int) alignof(float) char buffer[max(sizeof(int), sizeof(float))]; *(int*)buffer = n; // #...
Fureeish's user avatar
  • 12.6k
0 votes
4 answers
122 views

Can I assign or return a struct with unassigned elements in C?

In C, using an uninitialized variable is undefined behavior. Including: int x; int y = x; However, suppose I have the following: struct Struct { int a; int b; }; struct Struct s; s.a = 1; ...
user16217248's user avatar
  • 3,200
4 votes
0 answers
91 views

upcast and strict aliasing rule from C++20 [duplicate]

As per C++17 standard, §6.10 item 8: 8 If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined: (8.1) — the ...
shrike's user avatar
  • 4,489
6 votes
2 answers
312 views

Can you dereference a temporary array?

Consider the following code: void foo() { int arr[1]; *arr; // OK using T = int[1]; *T{}; // OK for Clang and MSVC // GCC error: taking address of temporary array } See ...
Jan Schultke's user avatar
  • 20.7k
10 votes
1 answer
193 views

Why does a variable refer to the variable outside of a lambda, if it is not odr used?

Look at this example (godbolt): void foo(int &par) { auto l = [par]() { decltype(par) x; }; } This program does not compile, because the par in decltype(par) refers to the ...
geza's user avatar
  • 28.6k
3 votes
1 answer
109 views

What is the type of a by-value lambda capture of a reference?

Look at this program (godbolt): template <typename> struct TD; void foo(int &par) { auto l = [par]() { TD<decltype(par)>(); }; } It intentionally doesn't compile, it ...
geza's user avatar
  • 28.6k

1
2 3 4 5
159