language-lawyer

POD implications for a struct which holds an standard library container

天大地大妈咪最大 提交于 2020-01-14 19:37:59
问题 I came across this question recently. My goal is to understand how the C++ compiler views struct definitions which hold standard library containers such as std::vector. Ben Voigt's answer to the linked question cites the following from the C++0x standard: .... A trivial class is a class that has a trivial default constructor (12.1) and is trivially copyable. [ Note: In particular, a trivially copyable or trivial class does not have virtual functions or virtual base classes. — end note ] A

Threshold an absolute value

白昼怎懂夜的黑 提交于 2020-01-14 08:50:29
问题 I have the following function: char f1( int a, unsigned b ) { return abs(a) <= b; } For execution speed, I want to rewrite it as follows: char f2( int a, unsigned b ) { return (unsigned)(a+b) <= 2*b; } // redundant cast Or alternatively with this signature that could have subtle implications even for non-negative b : char f3( int a, int b ) { return (unsigned)(a+b) <= 2*b; } Both of these alternatives work under a simple test on one platform, but I need it to portable. Assuming non-negative b

Reference initialization and constant expressions

牧云@^-^@ 提交于 2020-01-14 08:13:29
问题 As a followup to this question, gcc and clang both consider this program ill-formed: int main() { const int& ri = 0; constexpr int i = ri; } The error is about the value of ri being unusable in a constant expression. 0 is certainly a core constant expression, and as a prvalue core constant expression seems to satisfy these constraints (trivially, since int isn't of class, pointer, or array type). So shouldn't ri satisfy this criteria? The same is true if I use a prvalue literal of class type:

Can the storage of trivially copyable objects be safely reallocated with realloc?

喜夏-厌秋 提交于 2020-01-14 07:48:08
问题 I know that trivially copyable objects can safely be copied my malloc into an appropriate storage location 1 and that the destination object will have the same value as the source. Is this also possible with realloc ? That is, if realloc some storage containing some objects of type T , and realloc decides to move and copy the block, will the objects in the newly allocated storage be intact and have started their lifetime, and will the lifetime of the objects in the old storage be safely ended

Construction and initialization of trivial types in C++

强颜欢笑 提交于 2020-01-14 07:44:06
问题 A trivial class is trivially copyable and has a trivial default constructor (a trivial type is either one of those or a built-in class that works similarly). Since you can use memcpy to copy objects of trivial types, and since default-initializing trivial types doesn't change any of the bytes in the representation¹, does the following code (using C++20 concepts) correctly initialize a copy of the passed-in object? #include <cstdlib> #include <cstring> #include <new> #include <type_traits>

In S s = S() is it guaranteed that no temporary will be created?

橙三吉。 提交于 2020-01-14 07:19:44
问题 In the following code, are pS and s.pS guaranteed to be equal in the final line? In other words, in the statement S s = S(); , can I be sure that a temporary S will not be constructed? #include <iostream> using namespace std; struct S { S() { pS = this; } S* pS; }; int main() { S s = S(); S* pS = &s; cout << pS << " " << s.pS << endl; } In every compiler I've tested this in pS == s.pS , but I'm not sufficiently familiar with the standard to be able to satisfy myself that this is guaranteed.

std::less<void> and pointer types

元气小坏坏 提交于 2020-01-14 07:17:35
问题 std::less<T *> is guaranteed to provide total order, regardless of whether both pointers point into the same array. In the latest draft of the standard, is the same true for the transparent function object std::less<void> ( std::less<> ) when you call its operator() ? Obviously, the same question applies to std::greater , but I assume they are specified the same. 回答1: The current draft from github does not contain any language to that effect; in fact, its definition of less<> says explicitly

Is it undefined behavior to #define/#undef an identifier with special meaning?

那年仲夏 提交于 2020-01-14 07:15:49
问题 An answer to the question Disable check for override in gcc suggested using -Doverride= on the command line to disable errors for erroneous use of override, which is effectively the same as adding: #define override to the source file. My initial reaction was that this seems like undefined behavior since we are redefining a keyword but looking at the draft C++11 standard section 2.12 Keywords [lex.key] I was surprised that neither override nor final are keywords. They are covered in the

The rationale for some of the ADL algorithm steps (C++)

情到浓时终转凉″ 提交于 2020-01-14 04:08:31
问题 In short, I am trying to understand the behavior of Argument-Dependent Lookup in C++. Some statements in ISO/IEC 14882:2017 (E) regarding ADL are not clear to me. I hope somebody would clarify them to me. According to standard, ... Furthermore, if T is a class template specialization, its associated namespaces and classes also include: the namespaces and classes associated with the types of the template arguments provided for template type parameters (excluding template template parameters);

Is there any drawbacks in providing operator+ or operator- to bidirectional iterators?

夙愿已清 提交于 2020-01-13 12:08:28
问题 The bidirectional iterators have no luxuries like random access iterators, and hence need to depend upon the std::next and std::prev, when someone needs to do operations like std::set<int> s{ 1, 2, 3, 4, 5 }; //std::set<int> s2(s.cbegin(), s.cbegin() + 2); // won't work as there is no operator+ for std::set::const_iterator std::set<int> s2(s.cbegin(), std::next(s.cbegin(), 2)); //std::set<int> s3(s.cbegin(), s.cend() - 2); // won't work as there is no operator- for std::set::const_iterator