Tag: language-lawyer
-
Are `int main()` and `int main(void)` equivalent prototypes in C23?
11 C23 introduced new semantics in function declarators: 6.7.6.3 Function declarators […] 13 For a function declarator without a parameter type list: the effect is as if it were declared with a parameter type list consisting of the keyword void. A function declarator provides a prototype for the function. This seems to imply that a function…
-
Are enum values allowed in a std::integer_sequence?
14 This code compiles and executes fine using GCC 13 and Clang 17, but fails to compile on MSVC. I am wondering if the code is required to work according to the standard or if this is a problem with MSVC. Demo #include <utility> #include <iostream> enum e : int { A=5, B, C, D…
-
Is int &ref = ref; well formed
7 I have learned that evaluating an uninitialized variable is undefined behavior. In particular, int i = i; is undefined behavior. I have read What's the behavior of an uninitialized variable used as its own initializer? But, is using a reference variable to initialize itself also undefined behavior? In particular, is int &ref = ref;…
-
Which version of the C Standard Library does the C++23 Standard incorporate?
7 (My original question was going to be about "What happened to _BitInt?" but that was based on a misreading of some cppreference pages). The Library Introduction section 16.2 of the C++23 Draft Standard says that the C Standard library is supported in C++. The only reference to a specific C standard, however, is in…
-
Can one forward declare a function taking a vector of incomplete type with a default value?
6 Below code snippet demonstrates some real issue I faced recently in my program: #include<vector> class A; void f( const std::vector<A> & = {} ); There is an incomplete class A, and a function declaration taking a vector of A‘s with empty default value. And the function is not even called anywhere. It works fine…
-
Dependent name in local class
12 #include <type_traits> template<typename… Ts> void boo() { struct A { struct B {}; }; static_assert(std::is_class_v<A>); static_assert(std::is_class_v<A::B>); // compilation failure; need typename. } int main() { boo<int>(); } Why is A::B a dependent name and A is not? Can not see anything in standard about that. c++ language-lawyer Share Improve this question Follow edited 12…
-
std::enable_if_t works with gcc and clang but does not compile with msvc
8 I have a friend function template operator<< that works with both gcc and clang but not with msvc. #include <iostream> #include <type_traits> template< typename T, std::enable_if_t< T{1}, int> =0 > class Foo { template< typename Ar, typename R> friend Ar& operator<<(Ar& os, const Foo<R>& foo) { return os; } }; int main() { Foo<int>…
-
Why does ranges::for_each return the function?
10 The legacy std::for_each returns function as the standard only requires Function to meet Cpp17MoveConstructible according to [alg.foreach]: template<class InputIterator, class Function> constexpr Function for_each(InputIterator first, InputIterator last, Function f); Preconditions: Function meets the Cpp17MoveConstructible requirements. [Note: Function need not meet the requirements of Cpp17CopyConstructible. end note] This is reasonable since the user may want to…
-
Is it undefined behaviour to use pointer after allocated memory?
10 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, because data_length is zero. However buffer + buffer_length points just outside of the allocated memory. I wonder if it could trigger some kind of undefined behaviour? Should…
-
What happens when an assumption, i.e. [[assume]] contains UB?
6 In C++23, the [[assume(expression)]] attribute makes it so that if expression is false, the behavior is undefined. For example: int div(int x, int y) { [[assume(y == 1)]]; return x / y; } This compiles to the same code as if y was always 1. div(int, int): mov eax, edi ret However, what happens…