language-lawyer

Is the compiler allowed to optimise out private data members?

≯℡__Kan透↙ 提交于 2020-12-29 04:59:50
问题 If the compiler can prove that a (private) member of a class is never used, including by potential friends, does the standard allow the compiler to remove this member from the memory footprint of the class? It is self-evident that this not possible for protected or public members at compile time, but there could be circumstances where it is possible regarding private data members for such a proof to be constructed. Related questions: Behind the scenes of public, private and protected (sparked

Is incrementing/decrementing or adding an integer value to a pointer that is not pointing to an element in a sequence Undefined Behavior?

一曲冷凌霜 提交于 2020-12-29 03:51:25
问题 I know that pointers (to array element) and iterators can be incremented/decremented to walk a sequence of elements and can jump back-and-for elements in the sequence. But what will happen if I increment a pointer to a single object or add to it an integer value? is it undefined behavior or it is OK but we cannot access that memory? int x = 551; int* p = &x; ++p; --p; std::cout << *p << '\n'; Because I've already read that we should not increment/decrement a pointer that doesn't point to an

Access control in template parameters

亡梦爱人 提交于 2020-12-27 08:55:01
问题 Inspired from this answer, which claims to subvert the access control system, I wrote the following minimal version of the hack template<typename T> inline T memptr{}; template<auto Val> struct setptr { struct setter { setter() { memptr<decltype(Val)> = Val; } }; static setter s; }; template<auto Val> typename setptr<Val>::setter setptr<Val>::s{}; which is then used as class S { int i; }; template struct setptr<&S::i>; auto no_privacy(S& s) { return s.*memptr<int S::*>; } Why doesn't template

Access control in template parameters

自作多情 提交于 2020-12-27 08:53:09
问题 Inspired from this answer, which claims to subvert the access control system, I wrote the following minimal version of the hack template<typename T> inline T memptr{}; template<auto Val> struct setptr { struct setter { setter() { memptr<decltype(Val)> = Val; } }; static setter s; }; template<auto Val> typename setptr<Val>::setter setptr<Val>::s{}; which is then used as class S { int i; }; template struct setptr<&S::i>; auto no_privacy(S& s) { return s.*memptr<int S::*>; } Why doesn't template

Class type non-type template parameter initialization does not compile

孤人 提交于 2020-12-26 07:21:08
问题 I was under the impression that the following should become valid code under the new C++20 standard: struct Foo { int a, b; }; template<Foo> struct Bar {}; Bar<{.a=1, .b=2}> bar; Yet, gcc 10.2.0 , with -std=c++20 set complains: could not convert ‘{1, 2}’ from ‘<brace-enclosed initializer list>’ to ‘Foo’ and Clang cannot compile this snippet either. Can someone point out why it is not well formed? 回答1: This template-argument {.a=1, .b=2} is not allowed according to the grammar for a template

Class type non-type template parameter initialization does not compile

自古美人都是妖i 提交于 2020-12-26 07:19:07
问题 I was under the impression that the following should become valid code under the new C++20 standard: struct Foo { int a, b; }; template<Foo> struct Bar {}; Bar<{.a=1, .b=2}> bar; Yet, gcc 10.2.0 , with -std=c++20 set complains: could not convert ‘{1, 2}’ from ‘<brace-enclosed initializer list>’ to ‘Foo’ and Clang cannot compile this snippet either. Can someone point out why it is not well formed? 回答1: This template-argument {.a=1, .b=2} is not allowed according to the grammar for a template

Why can't you omit the array size in a new initializer?

只愿长相守 提交于 2020-12-25 01:52:34
问题 This is allowed: int a[]{1, 2, 3}; But not this: auto a = new int[]{1, 2, 3}; You have to specify the bounds. Why? EDIT: The proper syntax (that doesn't compile) is: auto a = new (int[]){1, 2, 3}; This gives the real error message, which is: error: invalid use of array with unspecified bounds 回答1: MSalters' answer addresses why this hasn't been changed in recent versions of the standard. Here I will answer the companion question, "where in the C++11 standard is this forbidden?" Regarding new

Why can't you omit the array size in a new initializer?

落爺英雄遲暮 提交于 2020-12-25 01:50:26
问题 This is allowed: int a[]{1, 2, 3}; But not this: auto a = new int[]{1, 2, 3}; You have to specify the bounds. Why? EDIT: The proper syntax (that doesn't compile) is: auto a = new (int[]){1, 2, 3}; This gives the real error message, which is: error: invalid use of array with unspecified bounds 回答1: MSalters' answer addresses why this hasn't been changed in recent versions of the standard. Here I will answer the companion question, "where in the C++11 standard is this forbidden?" Regarding new

Why can't you omit the array size in a new initializer?

落花浮王杯 提交于 2020-12-25 01:48:00
问题 This is allowed: int a[]{1, 2, 3}; But not this: auto a = new int[]{1, 2, 3}; You have to specify the bounds. Why? EDIT: The proper syntax (that doesn't compile) is: auto a = new (int[]){1, 2, 3}; This gives the real error message, which is: error: invalid use of array with unspecified bounds 回答1: MSalters' answer addresses why this hasn't been changed in recent versions of the standard. Here I will answer the companion question, "where in the C++11 standard is this forbidden?" Regarding new

Why can't you omit the array size in a new initializer?

China☆狼群 提交于 2020-12-25 01:46:31
问题 This is allowed: int a[]{1, 2, 3}; But not this: auto a = new int[]{1, 2, 3}; You have to specify the bounds. Why? EDIT: The proper syntax (that doesn't compile) is: auto a = new (int[]){1, 2, 3}; This gives the real error message, which is: error: invalid use of array with unspecified bounds 回答1: MSalters' answer addresses why this hasn't been changed in recent versions of the standard. Here I will answer the companion question, "where in the C++11 standard is this forbidden?" Regarding new