language-lawyer

I just can not understand DR 712

落爺英雄遲暮 提交于 2019-12-23 07:45:51
问题 DR 712 was responsible for the change in the wording of [basic.def.odr]/2 in C++11 to the current wording today, in [basic.def.odr]2 and 3. But I'm still trying to understand the reason for the change, as stated in the DR, as follows: 712. Are integer constant operands of a conditional-expression “used?” In describing static data members initialized inside the class definition, 9.2.3.2 [class.static.data] paragraph 3 says, The member shall still be defined in a namespace scope if it is used

Implicit synchronization when creating/joining threads

拥有回忆 提交于 2019-12-23 07:45:41
问题 What is the minimal framing required for x 's type for this code to work, considering the implied synchronization when creating/joining a thread: std::atomic ? volatile ? nothing? #include <thread> #include <cassert> int main() { int x = 123; // *** std::thread( [ & ] { assert( x == 123 ); x = 321; } ).join(); assert( x == 321 ); return 0; } 回答1: The invocation of std::thread 's constructor is synchronized and happens before the invocation of the copy of the thread function (30.3.1.2/6).

constexpr void function rejected

拜拜、爱过 提交于 2019-12-23 07:37:07
问题 I have this very simple function which won't compile. constexpr void func() { } The error I'm getting is: error: invalid return type ' void ' of constexpr function ' constexpr void func() ' constexpr void func() In C++14, void is a literal type [§3.9/10]: A type is a literal type if it is: void; or a scalar type; or a reference type; or an array of literal type; or a class type (Clause 9) that has all of the following properties: it has a trivial destructor, it is an aggregate type (8.5.1) or

Forward declare a constexpr variable template

微笑、不失礼 提交于 2019-12-23 07:21:41
问题 I tried to forward-declare a constexpr variable template like this: template<typename> constexpr std::size_t iterator_category_value; The goal was to document that every specialization should be constexpr but I have to admit that I never checked whether it was legal or not and g++ was happy with it. However, when I tried to compile this spinnet with clang++ instead, I got the following error: error: default initialization of an object of const type 'const std::size_t' (aka 'const unsigned

Is creating two FILEs for the same file descriptor well-defined?

旧巷老猫 提交于 2019-12-23 07:11:40
问题 POSIX specifies an fdopen function that creates a FILE for a file descriptor. POSIX also specifies a fileno function that returns the file descriptor for a FILE . Together, these two could be used to create a second FILE accessing the same underlying file descriptor as an existing file: FILE *secondfile(FILE *f, const char *mode) { int fd = fileno(f); return fd >= 0 ? fdopen(fd, mode) : NULL; } Is this a well-defined operation under POSIX? What happens when I access both the original FILE and

Use std::vector::data after reserve

倖福魔咒の 提交于 2019-12-23 07:03:20
问题 I have a std::vector on which I call reserve with a large value. Afterwards I retrieve data() . Since iterating data is then crashing I am wondering whether this is even allowed. Is reserve forced to update data to the allocated memory range? 回答1: The guarantee of reserve is that subsequent insertions do not reallocate, and thus do not cause invalidation. That's it. There are no further guarantees. 回答2: Is reserve forced to update data to the allocated memory range? No. The standard only

Why is this expression not a constant expression?

安稳与你 提交于 2019-12-23 07:01:28
问题 The expression b in this code shall be a core constant expression int main() { constexpr int a = 10; const int &b = a; constexpr int c = b; // here return 0; } since the standard says (8.20, paragraph 2 [expr.const] in n4700) An expression e is a core constant expression unless the evaluation of e would evaluate one of the following expressions: ... an lvalue-to-rvalue conversion (7.1) unless it is applied to ... a non-volatile glvalue that refers to a non-volatile object defined with

Can the following code be true for pointers to different things

这一生的挚爱 提交于 2019-12-23 07:01:05
问题 I have a piece of memory I am "guarding", defined by typedef unsigned char byte; byte * guardArea; size_t guardSize; byte * guardArea = getGuardArea(); size_t guardSize = getGuardSize(); An acceptable implementation for the sake of this would be: size_t glGuardSize = 1024; /* protect an area of 1kb */ byte * getGuardArea() { return malloc( glGuardSize ); } size_t getGuardSize() { return glGuardSize; } Can the following snippet return true for any pointer (from a different malloc, from the

Can the compiler exploit empty base optimisation if the class contains a member of the base class?

青春壹個敷衍的年華 提交于 2019-12-23 06:57:13
问题 Consider struct base {}; struct child : base {}; It's well-known that sizeof(child) can be 1 by application of the empty base optimisation . Now however, consider struct base {}; struct child : base {base b;}; Can the compiler apply the empty base optimisation now, or must sizeof(child) be at least 2? Reference: http://en.cppreference.com/w/cpp/language/ebo 回答1: No, it cannot. From the same reference: Empty base optimization is prohibited if one of the empty base classes is also the type or

Standard behavior for direct initialization of unsigned short

こ雲淡風輕ζ 提交于 2019-12-23 06:54:52
问题 I noticed today that in the example code: void print(unsigned short a) { std::cout << a << std::endl; } Initialization and use works like this: print(short (5)); But not like this: print(unsigned short(6)); main.cpp:16:8: error: expected primary-expression before 'unsigned' print(unsigned short(6)); And it's not to do with the type since this also works: typedef unsigned short ushort; print(ushort (6)); Live example. So I went searching for what the standard says about value initialization.