language-lawyer

What is return value from std::vector erase operator, according to the standard?

吃可爱长大的小学妹 提交于 2020-01-24 09:58:05
问题 I prefer to get info from the source, for this case this is ISO-IEC 14882, where erase method is described as the following: "iterator erase(const_iterator position); iterator erase(const_iterator first, const_iterator last); Effects: Invalidates iterators and references at or after the point of the erase. Complexity: The destructor of T is called the number of times equal to the number of the elements erased, but the move assignment operator of T is called the number of times equal to the

Can C++ classes with deleted methods be trivially copyable?

时光毁灭记忆、已成空白 提交于 2020-01-24 05:47:46
问题 I want class B to inherit all but a few methods of class A (which is assumed to be trivially copyable), and still be trivially copyable. In C++11 I can delete methods. Take for example: class A { // trivially copyable // private stuff here public: A& operator += (const A&); // other public stuff here }; class B: public A { public: B& operator += (const A&) = delete; }; Is B trivially copyable? I know there are issues regarding the deletion of special methods, but the compound assignment is

What does “possibly-hypothetical” mean in the pointer arithmetic rules?

邮差的信 提交于 2020-01-24 04:27:46
问题 In the standard's specification for pointer arithmetic ([expr.add]/4.2, we have: Otherwise, if P points to an array element i of an array object x with n elements ([dcl.array]), the expressions P + J and J + P (where J has the value j ) point to the (possibly-hypothetical) array element i + j of x if 0 ≤ i + j ≤ n and the expression P - J points to the (possibly-hypothetical) array element i − j of x if 0 ≤ i − j ≤ n . What does "possibly-hypothetical" mean here? The passage already

Template dependent false

时光总嘲笑我的痴心妄想 提交于 2020-01-23 14:27:07
问题 I have a class template which can't be used directly, only specializations are allowed. And I want to use static_assert to show meaningful error message. I can't just type static_assert(false, "error"); since false isn't value dependent and compiler may show error message even if the template is never used. My solution: template<class> struct AlwaysFalse : std::false_type{}; #define DEPENDENT_FALSE(arg) AlwaysFalse<decltype(sizeof(arg))>::value template<class T> struct Foo{ static_assert

Under what conditions is it safe to use std::memcpy to copy between objects?

不羁岁月 提交于 2020-01-23 13:03:17
问题 Under what set of conditions is it safe to use std::memcpy to copy from one object to another? For example, what conditions must T , src and dest satisfy for the following to be safe: template <typename T> void copy_bytewise(T& dest, const T& src) { std::memcpy(&dest, &src, sizeof(T)); } The only thing we can assume about src and dest is that they don't overlap 1 . In particular either of src or dest may be a reference to a member or base class. I am interested in answers which refer to the

Reliable type-punning across C and C++ standards

冷暖自知 提交于 2020-01-23 06:17:29
问题 Is there a way to type-pun that is valid in both C and C++? Preferably low overhead, and avoiding trivial preprocessor hacks. In C89, I know I can do something like this: unsigned int float_bits(float num) { return *(unsigned int *)&num; } However this violates C99's strict aliasing rule. So something like this might be more portable across the various C standards: unsigned int float_bits(float num) { union { float f; unsigned int i; } u; u.f = num; return u.i; } But I know that this is not

Can a pointer to an incomplete type be incomplete?

不羁岁月 提交于 2020-01-23 05:55:21
问题 Can int (*)[] be an incomplete type? C 2018 6.2.5 1 says: At various points within a translation unit an object type may be incomplete (lacking sufficient information to determine the size of objects of that type) or complete (having sufficient information). Thus it seems that if the size of a type is known, the type is complete. 6.2.6.1 28 specifies that certain types of pointers must have the same sizes (pointers to void and characters, pointers to compatible types, pointers to structures,

Can a pointer to an incomplete type be incomplete?

半城伤御伤魂 提交于 2020-01-23 05:55:09
问题 Can int (*)[] be an incomplete type? C 2018 6.2.5 1 says: At various points within a translation unit an object type may be incomplete (lacking sufficient information to determine the size of objects of that type) or complete (having sufficient information). Thus it seems that if the size of a type is known, the type is complete. 6.2.6.1 28 specifies that certain types of pointers must have the same sizes (pointers to void and characters, pointers to compatible types, pointers to structures,

Is zeroing out the “sockaddr_in” structure necessary?

六眼飞鱼酱① 提交于 2020-01-23 05:29:07
问题 Everywhere I look, I see the following piece of code: struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = ip; In C++, the same idea is usually expressed as sockaddr_in addr = {}; // unneccesary(?) value-initialzation addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = ip; Yet nowhere I look (in terms of official documentation) do I see any requirement to zero out the structure before

Use of variable in own initializer

余生长醉 提交于 2020-01-22 17:07:05
问题 [basic.scope.pdecl]/1 of the C++20 standard draft had the following (non-normative) example in a note (partial quote from before the merge of pull request 3580, see answer to this question): unsigned char x = x; [...] x is initialized with its own (indeterminate) value. Does this actually have well-defined behavior in C++20? Generally the self-initialization of the form T x = x; has undefined behavior by virtue of x 's value being indeterminate before initialization is completed. Evaluating