language-lawyer

Alias of a template. Who's right?

梦想的初衷 提交于 2020-05-13 04:47:12
问题 The following code seems reasonable, but doesn't work on two major compilers #include <type_traits> template<template<class> class Tmp> struct S{ template<class T> using tmp_t = Tmp<T>; static_assert(std::is_same_v< S<tmp_t>, S<Tmp> >, "Not same?? How come?"); }; template<class> struct Dummy{}; template struct S<Dummy>; gcc starting with 7.1 compiles alright ( https://godbolt.org/z/DjAcgP ) clang ( https://godbolt.org/z/ewBbZJ ) and msvc ( https://godbolt.org/z/6ZmQwj ) fail to do so Is this

Do Derived1::Base and Derived2::Base refer to the same type?

本小妞迷上赌 提交于 2020-05-12 11:38:01
问题 MSVC, Clang and GCC disagree on this code: struct Base { int x; }; struct Der1 : public Base {}; struct Der2 : public Base {}; struct AllDer : public Der1, public Der2 { void foo() { Der1::Base::x = 5; } }; Godbolt GCC: <source>: In member function 'void AllDer::foo()': <source>:10:21: error: 'Base' is an ambiguous base of 'AllDer' 10 | Der1::Base::x = 5; | ^ Compiler returned: 1 Clang gives a similar error, and MSVC gives no error. Who is right here? I suppose this is covered in [class

Forcing a decay of the array (for the lack of better title)

耗尽温柔 提交于 2020-05-12 02:24:11
问题 Inspired by this question: How does *(&arr + 1) - arr give the length in elements of array arr? The following code will calculate the length of an array, albeit invoking undefined behavior: int arr[] = {5, 8, 1, 3, 6}; size_t len = *(&arr + 1) - &arr[0]; // len is 5 I believe, that by dereferencing the (&arr + 1) we are triggering undefined behavior. However, the only reason we are doing this is to immediately decay the result into int* , pointing to one element after the last one in original

Resolving Definitions of Specialized Static Member Variables of Templated Classes

淺唱寂寞╮ 提交于 2020-05-11 05:22:10
问题 Compiler Fights XIV: Doom of the Duplicitous Double Definition, co-starring The Dubious Declaration! Compilers, all with either -O0 or Debug mode: g++ 5.2.0 clang++ 3.6.0 VC++ 18.00.40629 (MSVC 2013, Update 5) Summary: Is VC++ wrong in rejecting the declaration and definition of a specialized static member variable of a templated class with the syntax? template <> const std::string TemplatedClass::x; // .h file template <> const std::string TemplatedClass::x= "string"; // .cpp file Does

Why is std::hash not guaranteed to be deterministic?

不羁岁月 提交于 2020-05-09 20:15:09
问题 Hereafter, we use N4140 (C++14 Standard). According to § 17.6.3.4 Hash requirements , The value returned shall depend only on the argument k for the duration of the program . [ Note: Thus all evaluations of the expression h(k) with the same value for k yield the same result for a given execution of the program . — end note ] and § 20.9.12 Class template hash says ... the instantiation hash<Key> shall: (1.1) — satisfy the Hash requirements (17.6.3.4) ... (1.2) — ... This means a hash value of

Why is std::move not [[nodiscard]] in C++20?

早过忘川 提交于 2020-04-29 08:47:10
问题 I've recently read about [[nodiscard]] in C++17, and as far as I understand it's a new feature (design by contract?) which forces you to use the return value. This makes sense for controversial functions like std::launder (nodiscard since C++20), but I wonder why std::move isn't defined like so in C++17/20. Do you know a good reason or is it because C++20 isn't finalised yet? 回答1: AFAIK P0600R1 is the only proposal for adding [[nodiscard]] to the standard library that was applied to C++20.

Is the copy-list-initialization for class object a user-defined conversion

孤人 提交于 2020-04-17 22:54:25
问题 #include <iostream> struct Data{ Data(int){ } } int main(){ Data d = {0}; //#1 } As the above code show,Does the #1 invocation contain a user-defined conversion?In my understanding about the standard ,I think it does not For copy-list-initialization rules [dcl.init.list] Otherwise, if T is a class type, constructors are considered. The applicable constructors are enumerated and the best one is chosen through overload resolution ([over.match], [over.match.list]). If a narrowing conversion (see

Is the copy-list-initialization for class object a user-defined conversion

自作多情 提交于 2020-04-17 22:54:10
问题 #include <iostream> struct Data{ Data(int){ } } int main(){ Data d = {0}; //#1 } As the above code show,Does the #1 invocation contain a user-defined conversion?In my understanding about the standard ,I think it does not For copy-list-initialization rules [dcl.init.list] Otherwise, if T is a class type, constructors are considered. The applicable constructors are enumerated and the best one is chosen through overload resolution ([over.match], [over.match.list]). If a narrowing conversion (see

what are rules about when the return type is reference

为君一笑 提交于 2020-04-16 03:29:05
问题 #include <iostream> int func0(){ int a = 0; return a; } int&& func1(){ int a = 0; return a; } int main(){ int&& result0 = func0(); int&& result1 = func1(); } return statement rules are: A function returns to its caller by the return statement. [...] the return statement initializes the glvalue result or prvalue result object of the (explicit or implicit) function call by copy-initialization from the operand. The rule about how to initialize the object of the function call is only #2. We know

Is it legal write to a byte array in a union and read from an int to convert values in MISRA C?

跟風遠走 提交于 2020-04-13 08:05:11
问题 I guess this must have been asked before, but I could not get a specific yes/no answer. I have this code snippet : union integer_to_byte { signed int IntPart; unsigned char BytePart[2]; }; typedef union integer_to_byte I2B; main() { I2B u16VarNo; while(1) { // some code.... u16VarNo.BytePart[1]= P1; // some more code .... u16VarNo.BytePart[0]= P2; // still more code ... if(u16VarNo.IntPart != 0xFFFF) { } } } Is this a legal way to use Unions in C ? From what I read; only the last assigned