language-lawyer

Why does a struct, that has another struct wrapped in a union as a member not compile without an explicit default constructor?

房东的猫 提交于 2019-12-23 17:01:39
问题 This is the relationship I am talking about: struct A{ int i = 1; }; struct B{ union{A a;}; }; void main(){ B b; }; In this constellation, my compiler (vs2015) complains about the default constructor of B B::B(void) beeing deleted, with the note that the compiler has generated B::B : ../test.cpp(155): error C2280: "B::B(void)" : Es wurde versucht, auf eine gelöschte Funktion zu verweisen ../test.cpp(152): note: Compiler hat hier "B::B" generiert (sorry, I could not convince msvc to talk

Implementing the Linux Kernel's __is_constexpr (ICE_P) macro in pure C++

爷,独闯天下 提交于 2019-12-23 16:42:04
问题 After reading about the standard C11 version of Martin Uecker's ICE_P predicate, I tried to implement it in pure C++. The C11 version, making use of _Generic selection is as follows: #define ICE_P(x) _Generic((1? (void *) ((x)*0) : (int *) 0), int*: 1, void*: 0) The obvious approach for C++ is to replace _Generic by a template and decltype , such as: template<typename T> struct is_ice_helper; template<> struct is_ice_helper<void*> { enum { value = false }; }; template<> struct is_ice_helper

Deferred initialisation order in C++11

匆匆过客 提交于 2019-12-23 16:13:38
问题 Consider the following code, split across three compilation units: a.h : struct A { void Register(const char* s); const char* m_s[10]; int m_i = 0; }; A& GetA(); a.cpp : #include "a.h" #include <stdio.h> void A::Register(const char* s) { m_s[m_i++] = s; } A& GetA() { static A instance; return instance; } int main(int argc, char* argv[]) { A& a = GetA(); int n = a.m_i; for (int i = 0; i < n ; ++i) printf("%s\n", a.m_s[i]); return 0; } b.cpp : #include "a.h" struct B { B() { GetA().Register("b"

Comma operator in C++11 (sequencing)

China☆狼群 提交于 2019-12-23 15:43:07
问题 The standard mentions f(a,(t=3,t+2),c); which would be an assignment-expression followed by an expression for the 2nd operator according to my understanding. But the grammar lists it juxtaposed: expression: assignment-expression expression, assignment-expression Working Draft, Standard for Programming Language C ++ Revision N4140 (November 2014) Is someone so nice as to explain to me please what it is that I'm missing here? 回答1: When you see expression: assignment-expression expression,

Can technically objects occupy non-contiguous bytes of storage?

时光毁灭记忆、已成空白 提交于 2019-12-23 15:18:16
问题 While answering this question I was asked to provide standard quotes. I was shocked to find in the C++14 draft: § 3.9 Types [basic.types] The object representation of an object of type T is the sequence of N unsigned char objects taken up by the object of type T, where N equals sizeof(T) Hmm.. it doesn't say that the "unsigned char objects" must be contiguous in memory. Maybe it is implied by "sequence". Then I found a specific mention of "contiguous bytes of storage", but... § 1.8 The C++

Optimization allowed on volatile objects

元气小坏坏 提交于 2019-12-23 13:57:49
问题 From ISO/IEC 9899:201x section 5.1.2.3 Program execution paragraph 4 : In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced ( including any caused by calling a function or accessing a volatile object ). What exactly is the allowed optimization here regarding the volatile object? can someone give an example of

C++ full-expression standard wording

随声附和 提交于 2019-12-23 13:24:07
问题 In paragraph 12 of [intro.execution] in the current (C++ 17) C++ standard draft there is written: A full-expression is: [...] an expression that is not a subexpression of another expression and that is not otherwise part of a full-expression. If a language construct is defined to produce an implicit call of a function, a use of the language construct is considered to be an expression for the purposes of this definition. [...] The "use of the language construct" wording refers to the fact that

Securing CRTP: is private destructor the only solution?

丶灬走出姿态 提交于 2019-12-23 13:12:04
问题 How to avoid template <typename Derived> struct base { int foo() { return static_cast<Derived*>(this)->bar(); } }; struct derived : base<derived> { int bar(); }; struct another_derived : base<derived> { int bar(); }; // error: wrong base without additional code in derived classes ? This has been asked twice before (though without the extra condition of avoiding additional code in derived classes), with the recommended answer template <typename Derived> struct base { int foo() { return static

Securing CRTP: is private destructor the only solution?

拟墨画扇 提交于 2019-12-23 13:09:24
问题 How to avoid template <typename Derived> struct base { int foo() { return static_cast<Derived*>(this)->bar(); } }; struct derived : base<derived> { int bar(); }; struct another_derived : base<derived> { int bar(); }; // error: wrong base without additional code in derived classes ? This has been asked twice before (though without the extra condition of avoiding additional code in derived classes), with the recommended answer template <typename Derived> struct base { int foo() { return static

Value category of conditional operator

老子叫甜甜 提交于 2019-12-23 12:58:28
问题 Consider the following code: int x; int& f() { return x ? x : throw 0; } With gcc version 7.3.0 (Ubuntu 7.3.0-27ubuntu1~18.04) I get the following compilation error: cannot bind non-const lvalue reference of type ‘int&’ to an rvalue of type ‘int’ Note that this compiles just fine in clang. Here is (what I believe to be) the relevant statement from the standard: N4659 [8.16.2.1] (Conditional Operator): The second or the third operand (but not both) is a (possibly parenthesized) throw