language-lawyer

Is it possible to get a pointer to one subobject via a pointer to a different, unreleated subobject?

浪尽此生 提交于 2020-06-22 11:05:34
问题 Look at this simple code: struct Point { int x; int y; }; void something(int *); int main() { Point p{1, 2}; something(&p.x); return p.y; } I expect, that main 's return value can be optimized to return 2; , as something doesn't have access to p.y , it only gets a pointer to p.x . But, none of the major compilers optimize the return value of main to 2 . Godbolt. Is there something in the standard, which allows something to modify p.y , if we only give access to p.x ? If yes, does this depend

Is it UB to access a member by casting an object pointer to `char *`, then doing `*(member_type*)(pointer + offset)`?

↘锁芯ラ 提交于 2020-06-22 10:14:07
问题 Here's an example: #include <cstddef> #include <iostream> struct A { char padding[7]; int x; }; constexpr int offset = offsetof(A, x); int main() { A a; a.x = 42; char *ptr = (char *)&a; std::cout << *(int *)(ptr + offset) << '\n'; // Well-defined or not? } I always assumed that it's well-defined (otherwise what would be the point of offsetof ), but wasn't sure. Recently I was told that it's in fact UB, so I want to figure it out once and for all. Does the example above cause UB or not? If

Is it UB to access a member by casting an object pointer to `char *`, then doing `*(member_type*)(pointer + offset)`?

我与影子孤独终老i 提交于 2020-06-22 10:14:06
问题 Here's an example: #include <cstddef> #include <iostream> struct A { char padding[7]; int x; }; constexpr int offset = offsetof(A, x); int main() { A a; a.x = 42; char *ptr = (char *)&a; std::cout << *(int *)(ptr + offset) << '\n'; // Well-defined or not? } I always assumed that it's well-defined (otherwise what would be the point of offsetof ), but wasn't sure. Recently I was told that it's in fact UB, so I want to figure it out once and for all. Does the example above cause UB or not? If

Why does it make a difference if left and right shift are used together in one expression or not?

百般思念 提交于 2020-06-22 08:21:30
问题 I have the following code: unsigned char x = 255; printf("%x\n", x); // ff unsigned char tmp = x << 7; unsigned char y = tmp >> 7; printf("%x\n", y); // 1 unsigned char z = (x << 7) >> 7; printf("%x\n", z); // ff I would have expected y and z to be the same. But they differ depending on whether a intermediary variable is used. It would be interesting to know why this is the case. 回答1: This little test is actually more subtle than it looks as the behavior is implementation defined: unsigned

Using `reinterpret_cast` on an enum class - valid or undefined behavior?

北战南征 提交于 2020-06-17 06:47:51
问题 #include <iostream> #include <cassert> #include <type_traits> template<typename T> using Underlying = std::underlying_type_t<T>; enum class ETest : int { Zero = 0, One = 1, Two = 2 }; template<typename T> auto& castEnum(T& mX) noexcept { // `static_cast` does not compile // return static_cast<Underlying<T>&>(mX); return reinterpret_cast<Underlying<T>&>(mX); } int main() { auto x(ETest::Zero); castEnum(x) = 1; assert(x == ETest::One); return 0; } ideone Is this code guaranteed to always work?

Is an init-declarator a prvalue expression

一曲冷凌霜 提交于 2020-06-16 23:48:20
问题 int c = 0; Consider the above code,thereof, c = 0 is an init-declarator and it's also an expression,Becuase of these rules: init-declarator: declarator initializer(opt) A full-expression is: [...] an init-declarator or a mem-initializer, including the constituent expressions of the initializer, As long as an expression,it will have a value category. A prvalue is an expression whose evaluation initializes an object or a bit-field, or computes the value of the operand of an operator, as

Is an init-declarator a prvalue expression

强颜欢笑 提交于 2020-06-16 23:45:09
问题 int c = 0; Consider the above code,thereof, c = 0 is an init-declarator and it's also an expression,Becuase of these rules: init-declarator: declarator initializer(opt) A full-expression is: [...] an init-declarator or a mem-initializer, including the constituent expressions of the initializer, As long as an expression,it will have a value category. A prvalue is an expression whose evaluation initializes an object or a bit-field, or computes the value of the operand of an operator, as

The question about a glvalue constant expression used in a context that requires a constant expression

*爱你&永不变心* 提交于 2020-06-16 08:02:52
问题 #include <iostream> constexpr int func2(int const& id){ return id; } template<int v> struct Test{ }; int main(){ const int v = 0; Test<func2(v)> c; } Consider the above code,I just don't understand why the code is well-formed.My pointview is that the name v is used as a glvalue when evalute expression func2 ,becuase the parameter of func2 is of reference type,the v need to be bound to the id-expression id .So we look at the requirement of a glvalue constant expression,here are quotes about

Can you use using to redeclare a public member in base class as private in derived class?

不问归期 提交于 2020-06-16 03:37:08
问题 This code snippet demonstrating changing class member access came from IBM. struct A { protected: int y; public: int z; }; struct B : private A { }; struct C : private A { public: using A::y; using A::z; }; struct D : private A { protected: using A::y; using A::z; }; struct E : D { void f() { y = 1; z = 2; } }; struct F : A { public: using A::y; private: using A::z; }; int main() { B obj_B; // obj_B.y = 3; // obj_B.z = 4; C obj_C; obj_C.y = 5; obj_C.z = 6; D obj_D; // obj_D.y = 7; // obj_D.z

What does [decl.constexpr].5 mean exactly?

谁说胖子不能爱 提交于 2020-06-16 02:26:30
问题 The standard on constexpr functions states under point 5 of [decl.constexpr]: For a non-template, non-defaulted constexpr function or a non-template, non-defaulted, non-inheriting constexpr constructor, if no argument values exist such that an invocation of the function or constructor could be an evaluated subexpression of a core constant expression (5.19), the program is ill-formed; no diagnostic required. It goes on to give the following example for this: constexpr int f(bool b){ return b ?