language-lawyer

Why GCC 5.3.0 gives warning when binding reference to “this” pointer

∥☆過路亽.° 提交于 2020-01-12 12:51:44
问题 Here is the minimal example: class A { A* const& this_ref; public: A() : this_ref(this) {} }; GCC 5.3.0 gives warning: warning: a temporary bound to 'A::this_ref' only persists until the constructor exits [-Wextra] A() : this_ref(this) {} Is this a temporary then? What the... MSVC 2015 is silent about this, and referring to class members by this_ref->member outside the constructor in my case gives expected behaviour (but might be just a case of UB, not sure). EDIT: Note this question extends

What is the Relationship Between the C and C++ Standards?

泄露秘密 提交于 2020-01-12 11:10:50
问题 I was writing this answer and I quoted from http://en.cppreference.com/w/cpp/string/byte/tolower#Parameters Is not representable as unsigned char and does not equal EOF, the behavior is undefined When I went to inspect the edit that had added this phrase I found that the author's comment: Can't use negative signed chars with any ctype.h function per C99 7.4/1 The author is citing from the C99 standard in C++ documentation. Is that valid? I couldn't find anything on the definition of this

What is the Relationship Between the C and C++ Standards?

人走茶凉 提交于 2020-01-12 11:09:42
问题 I was writing this answer and I quoted from http://en.cppreference.com/w/cpp/string/byte/tolower#Parameters Is not representable as unsigned char and does not equal EOF, the behavior is undefined When I went to inspect the edit that had added this phrase I found that the author's comment: Can't use negative signed chars with any ctype.h function per C99 7.4/1 The author is citing from the C99 standard in C++ documentation. Is that valid? I couldn't find anything on the definition of this

What is the Relationship Between the C and C++ Standards?

我的未来我决定 提交于 2020-01-12 11:09:27
问题 I was writing this answer and I quoted from http://en.cppreference.com/w/cpp/string/byte/tolower#Parameters Is not representable as unsigned char and does not equal EOF, the behavior is undefined When I went to inspect the edit that had added this phrase I found that the author's comment: Can't use negative signed chars with any ctype.h function per C99 7.4/1 The author is citing from the C99 standard in C++ documentation. Is that valid? I couldn't find anything on the definition of this

A virtual member function is used if it is not pure?

被刻印的时光 ゝ 提交于 2020-01-12 07:42:07
问题 C++03 3.2.2 ...An object or non-overloaded function is used if its name appears in a potentially-evaluated expression. A virtual member function is used if it is not pure... And then later in 3.2.3 we have: Every program shall contain exactly one definition of every non-inline function or object that is used in that program; no diagnostic required. The definition can appear explicitly in the program, it can be found in the standard or a user-defined library, or (when appropriate) it is

Can I use rvalue reference to temporary? Is it undefined behavior or not?

删除回忆录丶 提交于 2020-01-12 06:50:09
问题 Updating the question Why this two rvalue references examples have different behavior?: Source code: int a = 0; auto && b = a++; ++a; cout << a << b << endl; prints 20 Is it undefined behavior (UB) to use b after the a++ call? Maybe we cannot use b because it refers to a temporary? 回答1: No it is not undefined behavior (UB). It's fine - you can modify the contents of the temporary here (so long as the reference is valid for the lifetime of the temporary, in this case the bind to the rvalue

constexpr exp, log, pow

孤者浪人 提交于 2020-01-12 06:26:10
问题 I'd like to use constexpr versions of standard <cmath> functions like exp , log , pow in a portable way. I currently have a non-portable solution g++ treats these functions as constexpr - a non-compliant extension of C++, but I am concerned about portability and future-proofing (I imagine that this extension might one day be removed from g++ ). I am interested in constexpr versions of these functions, not template metaprograms - I want the same functionality to be available both at compile

Why are mutexes and condition variables trivially copyable?

大憨熊 提交于 2020-01-12 04:54:07
问题 LWG 2424 discusses the undesirable status of atomics, mutexes and condition variables as trivially copyable in C++14. I appreciate that a fix is already lined up, but std::mutex, std::condition variable et al. appear to have non-trivial destructors. For example: 30.4.1.2.1 Class mutex [thread.mutex.class] namespace std { class mutex { public: constexpr mutex() noexcept; ~mutex(); // user-provided => non-trivial … } } Shouldn't this disqualify them as trivially copyable? 回答1: Either it was my

Rvalues, lvalues and formal definitions

独自空忆成欢 提交于 2020-01-12 00:53:28
问题 People are confused when they hear that in int&& x x has rvalue reference type, but x is an lvalue. Misunderstanding stems from the fact that identifiers and expressions are different things, and so are types and value categories. Moreover, types of expressions are "adjusted prior to any further analysis", and the words "rvalue" and "lvalue" can appear both in type name and in value category name. I want to clarify formal definitions. Suppose we have a function: 1 | void f(int&& x) { 2 | ...

Flexible array members can lead to undefined behavior?

夙愿已清 提交于 2020-01-11 17:08:31
问题 By using flexible array members (FAMs) within structure types, are we exposing our programs to the possibility of undefined behavior? Is it possible for a program to use FAMs and still be a strictly conforming program? Is the offset of the flexible array member required to be at the end of the struct? The questions apply to both C99 (TC3) and C11 (TC1) . #include <stdio.h> #include <stdlib.h> #include <stddef.h> int main(void) { struct s { size_t len; char pad; int array[]; }; struct s *s =