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

This is perfectly well-defined: void something(int *x) { reinterpret_cast<Point*>(x)->y = 42; } The Point object (p) and its x member are pointer-interconvertible, from [basic.compound]: Two objects a and b are pointer-interconvertible if: […] one is a standard-layout class object and the other is the first non-static data member of that object, or, if the object … Read more

Is it possible in modern C++ to pass a string literal as a parameter to a C++ template?

Yes, in c++20. The problem was that determining uniqueness of a template non-type argument was difficult. c++20 adds in a <=> spaceship operator comparison. If it is non-user provided (and based only off non-user provided <=> in turn, repeat recursively) (and a few other requirements; see p0732), the type can be used as a non-type … Read more

Is std::memcpy between different trivially copyable types undefined behavior?

The standard may fail to say properly that this is allowed, but it’s almost certainly supposed to be, and to the best of my knowledge, all implementations will treat this as defined behaviour. In order to facilitate the copying into an actual char[N] object, the bytes making up the f object can be accessed as … Read more

Ternary operator implicit cast to base class

Quoting from C++ standard draft N4296, Section 5.16 Conditional operator, Paragraph 6.3: One or both of the second and third operands have pointer type; pointer conversions (4.10) and qualification conversions (4.4) are performed to bring them to their composite pointer type (Clause 5). The result is of the composite pointer type. Section 5 Expressions, Paragraph … Read more

How to assert that a constexpr if else clause never happen?

You have to make the discarded statement dependent of the template parameters template <class…> constexpr std::false_type always_false{}; if constexpr(condition1){ … } else if constexpr (condition2) { …. } else if constexpr (condition3) { …. } else { static_assert(always_false<T>); } This is so because [temp.res]/8 – The program is ill-formed, no diagnostic required, if no valid … Read more

What is the value of __cplusplus for C++17?

tl;dr: For C++17, __cplusplus is 201703L. What is the value of __cplusplus when using C++17? According to the draft standard N4594 §16.8/p1 Predefined macro names [cpp.predefined] (Emphasis Mine): The following macro names shall be defined by the implementation: __cplusplus The name __cplusplus is defined to the value 201402L when compiling a C++ translation unit.156 156) … Read more

Why do C++17 structured bindings not use { }?

The National Bodies from Spain and US have proposed to change back to the {} syntax because (P0488R0): The “structured bindings” proposal originally used braces “{}” to delimit binding identifiers. Those delimiters were changed to brackets “[]” under the assertion that they didn’t introduce any syntactic problem. However, they turned out to introduce syntactic ambiguity … Read more

C++ std::variant vs std::any

The more things you check at compile time the fewer runtime bugs you have. variant guarantees that it contains one of a list of types (plus valueless by exception). It provides a way for you to guarantee that code operating on it considers every case in the variant with std::visit; even every case for a … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)