language-lawyer

In the C++ standard, what is specified to occur when a “shall” requirement is violated?

痞子三分冷 提交于 2019-12-18 14:08:43
问题 For example, the famous words (§3.2/1) No translation unit shall contain more than one definition of any variable, function, class type, enumeration type, or template. I believe "shall" requirements are to be interpreted as though they are implicitly followed by "otherwise the program is ill-formed" unless otherwise specified. However, others claim that "shall" instead means "otherwise the behavior is undefined". In every case I've come across in the standard in which a "shall" requirement

using vs. typedef - is there a subtle, lesser known difference?

淺唱寂寞╮ 提交于 2019-12-18 14:03:00
问题 Background Everybody agrees that using <typedef-name> = <type>; is equivalent to typedef <type> <typedef-name>; and that the former is to be preferred to the latter for various reasons (see Scott Meyers, Effective Modern C++ and various related questions on stackoverflow). This is backed by [dcl.typedef]: A typedef-name can also be introduced by an alias-declaration. The identifier following the using keyword becomes a typedef-name and the optional attribute-specifier-seq following the

reinterpret_cast, char*, and undefined behavior

☆樱花仙子☆ 提交于 2019-12-18 13:06:48
问题 What are the cases where reinterpret_cast ing a char* (or char[N] ) is undefined behavior, and when is it defined behavior? What is the rule of thumb I should be using to answer this question? As we learned from this question, the following is undefined behavior: alignas(int) char data[sizeof(int)]; int *myInt = new (data) int; // OK *myInt = 34; // OK int i = *reinterpret_cast<int*>(data); // <== UB! have to use std::launder But at what point can we do a reinterpret_cast on a char array and

Are C++ standard library implementations allowed to strengthen noexcept specifications?

本秂侑毒 提交于 2019-12-18 12:54:29
问题 According to the C++ standard, are implementations of the C++ standard library allowed to strengthen noexcept specifications of methods and other functions of the C++ standard library as defined by the standard? For example, if the C++ standard specifies some function std::f as void f(); are standard library implementations allowed to implement it as void f() noexcept; instead? 回答1: The Standard says yes: § 17.6.5.12.1 Restrictions on exception handling [res.on.exception.handling] Any of the

Is the comma operator allowed in a constant-expression in C++11?

耗尽温柔 提交于 2019-12-18 12:47:49
问题 In the process of answering this question on SO for C++11, I realized that in C++03 (as well as in C) the use of the comma operator is explicitly forbidden in a constant-expression . Paragraph 5.19/1 of the C++03 Standard on constant expressions says: [...] In particular, except in sizeof expressions, functions, class objects, pointers, or references shall not be used, and assignment, increment, decrement, function-call, or comma operators shall not be used . In C++11, however, that last part

Is it UB to cast away const and read value? [duplicate]

筅森魡賤 提交于 2019-12-18 12:35:13
问题 This question already has an answer here : Does removing const from a pointer-to-const obey strict aliasing in C, and refer to the same object? (1 answer) Closed last year . Clarification: My question is: Is it UB to use an lvalue of type int to access an object of effective type const int ? This question has two code samples which use an lvalue of type int to access an object of effective type const int , and my intent is to achieve this with as little distraction as possible. If there is

Different behavior for qualified and unqualified name lookup for template

不羁岁月 提交于 2019-12-18 12:28:08
问题 How should this code behave? It calls generic function ignoring my overload if I use qualified name in call_read() function; and it calls overload first and then generic version if I use unqualified name. What's the difference? Is it a bug in GCC? #include <iostream> struct info1 {}; struct info2 {}; template<class T> void read(T& x) { std::cout << "generic" << std::endl; } template<class T> void call_read(T& x) { ::read(x); // if I replace ::read(x) with read(x) the overload is called } void

Undefined behavior and temporaries

会有一股神秘感。 提交于 2019-12-18 12:24:55
问题 1) Is it undefined behavior to return a reference to a temporary, even if that reference is not used? For example, is this program guaranteed to output "good": int& func() { int i = 5; return i; } int main() { func(); cout << "good" << endl; return 0; } 2) Is it undefined behavior to simply have a reference to an object that no longer exists, even if that reference is not used? For example, is this program guaranteed to output "good": int main() { int *j = new int(); int &k = *j; delete j;

Calling `this` member function from generic lambda - clang vs gcc

蹲街弑〆低调 提交于 2019-12-18 12:06:28
问题 Issue: passing a generic lambda (to a template function) that captures this and calls a member function of this without an explicit this-> does not compile on gcc. If the lambda is not generic, or if the lambda is not passed to any other function but called in place, it compiles withoit an explicit this-> . Clang is cool with the code in all situations. Time for another round of clang vs gcc . Who's right? Wandbox example template<typename TF> void call(TF&& f) { f(1); } struct Example { void

Initializing capturing lambda in ternary operator

北慕城南 提交于 2019-12-18 11:45:48
问题 I wanted to create a lambda in the following way: auto l1 = condition ? [](){ return true; } : [number](){ return number == 123; }; However, I got error: operands to ?: have different types ‘main()::<lambda()>’ and ‘main()::<lambda()>’ Obviously, the types seem to be the same. I thought, that capturing number in only one of lambdas might be a problem, but I get the same error for these: //check if capturing number in both lambdas would help auto l2 = condition ? [number](){ return true; } :