language-lawyer

Partial template specialization with mismatching `int` and `size_t` not compiling

百般思念 提交于 2020-03-21 11:33:06
问题 With reference to the following code #include <utility> #include <cassert> template <typename T> struct Wot; template <int... ints> struct Wot<std::index_sequence<ints...>> {}; int main() { assert(sizeof(Wot<std::index_sequence<1, 2, 3>>) == 1); } This works on clang but does not work on gcc, when I change the type of the partial specialization to accept std::size_t in the index sequence however it works. Who is right? Clang or gcc? See this in action here https://wandbox.org/permlink

Multiple inconsistent behavior of signed bit-fields

匆匆过客 提交于 2020-03-18 12:30:28
问题 I have come across a strange behavior on signed bit-fields: #include <stdio.h> struct S { long long a31 : 31; long long a32 : 32; long long a33 : 33; long long : 0; unsigned long long b31 : 31; unsigned long long b32 : 32; unsigned long long b33 : 33; }; long long f31(struct S *p) { return p->a31 + p->b31; } long long f32(struct S *p) { return p->a32 + p->b32; } long long f33(struct S *p) { return p->a33 + p->b33; } int main() { struct S s = { -2, -2, -2, 1, 1, 1 }; long long a32 = -2;

Can a trivial type class be copied when not all its members are initialized?

笑着哭i 提交于 2020-03-18 09:42:31
问题 (I just realized I first need to solve a much more basic issue with copying unions: When a union object is copied, is a member subobject created?. Please see that other question first.) The implicitly generated copy operations (constructor and assignment) of a class perform member by member copy (initialization or assignment). (For a trivial type these are the same.) So a class with some members not initialized cannot be copied, as accessing uninitialized objects is illegal. struct C { int m1

Invoking `constexpr` member function through reference - clang vs gcc

梦想的初衷 提交于 2020-03-18 05:03:09
问题 Consider the following example ( snippet (0) ): struct X { constexpr int get() const { return 0; } }; void foo(const X& x) { constexpr int i = x.get(); } int main() { foo(X{}); } The above example compiles with all versions of g++ prior to g++ 10.x , and never compiled under clang++ . The error message is: error: 'x' is not a constant expression 8 | constexpr int i = x.get(); | live example on godbolt.org The error kind of makes sense, as x is never a constant expression in the body of foo ,

Can there be different implicit objects based on a later runtime decision in C++20?

回眸只為那壹抹淺笑 提交于 2020-03-18 03:52:30
问题 This question refers to the addition of P0593 to the latest C++20 draft . Here is my example: #include <cstdlib> #include <cstdio> void foo(void *p) { if ( std::getchar() == 'i' ) { *(int *)p = 2; std::printf("%d\n", *(int *)p); } else { *(float *)p = 2; std::printf("%f\n", *(float *)p); } } int main() { void *a = std::malloc( sizeof(int) + sizeof(float) ); if ( !a ) return EXIT_FAILURE; foo(a); // foo(a); [2] } Is this code well-defined for all inputs under the latest draft? The rationale

Is it safe to call the functions from <cctype> with char arguments?

狂风中的少年 提交于 2020-03-17 11:55:32
问题 The C programming language says that the functions from <ctype.h> follow a common requirement: ISO C99, 7.4p1: In all cases the argument is an int , the value of which shall be representable as an unsigned char or shall equal the value of the macro EOF . If the argument has any other value, the behavoir is undefined. This means that the following code is unsafe: int upper(const char *s, size_t index) { return toupper(s[index]); } If this code is executed on an implementation where char has

`noexcept` behavior of `constexpr` functions

你。 提交于 2020-03-17 11:33:30
问题 The wording of [expr.unary.noexcept] changed in C++17 (edited following a comment by @L.F.: C++20 ) . Previously (n4140, 5.3.7 noexcept operator [expr.unary.noexcept]), my emphasis : The result of the noexcept operator is false if in a potentially-evaluated context the expression would contain (3.1) a potentially-evaluated call to a function, member function, function pointer, or member function pointer that does not have a non-throwing exception-specification ([except.spec]), unless the call

Class with iter but not next

ぃ、小莉子 提交于 2020-03-16 07:54:52
问题 For the iterator protocol you create both an __iter__ and __next__ method. However, what about the following case: class Item: def __init__(self): self.name = 'James' def __iter__(self): return self Now I can do: >>> i=Item() >>> iter(i) <__main__.Item instance at 0x10bfe6e18> But not: >>> next(i) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: instance has no next() method As far as I'm aware, the definition of iterator/iterable is: Iterable has the method _

C null pointer arithmetic

旧街凉风 提交于 2020-03-16 06:35:38
问题 I noticed this warning from Clang: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic] In details, it is this code which triggers this warning: uint8_t *end = ((uint8_t*)0) + sizeof(uint8_t) * count; Why would arithmetic on a null pointer be forbidden when doing the same on a non-null pointer obtained from an integer different than zero does not trigger any warning ? And more importantly, does the C standard explicitly forbid null

Allocating memory for a part of structure

我与影子孤独终老i 提交于 2020-03-14 07:04:50
问题 I have the following example #include <stdlib.h> #include <stdio.h> #include <stddef.h> typedef struct test{ int a; long b; int c; } test; int main() { test *t = (test*) malloc(offsetof(test, c)); t -> b = 100; } It works fine, but Im not sure about it. I think I have UB here. We have a pointer to an object of a structure type. But the object of the structure type is not really valid. I went through the standard and could not find any definition of this behavior. The only section I could find