language-lawyer

does in c++ the conversion from unsigned int to int always preserve the bit pattern?

一个人想着一个人 提交于 2019-12-21 03:43:19
问题 From the standard (4.7) it looks like the conversion from int to unsigned int, when they both use the same number of bits, is purely conceptual: If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2 n where n is the number of bits used to represent the unsigned type). [ Note: In a two’s complement representation, this conversion is conceptual and there is no change in the bit pattern (if there is no truncation). — end

Does C++, as an abstraction, support “bits” representing one of more than two values?

人盡茶涼 提交于 2019-12-21 03:42:54
问题 [C++11: 1.7] talks about bytes in terms of bits: The fundamental storage unit in the C++ memory model is the byte. A byte is at least large enough to contain any member of the basic execution character set (2.3) and the eight-bit code units of the Unicode UTF-8 encoding form and is composed of a contiguous sequence of bits, the number of which is implementation-defined. The least significant bit is called the low-order bit; the most significant bit is called the high-order bit. The memory

What happens when an exception is thrown while computing a constexpr?

妖精的绣舞 提交于 2019-12-21 03:33:26
问题 When computing constant expressions to initialize a constexpr it is possible to throw exceptions. For example here is an example where the computation of a constant expression is guarded against overflow: #include <iostream> #include <stdexcept> constexpr int g(int n, int n0, int n1) { return n == 0? n1: g(n - 1, n1, n0 + n1); } constexpr int f(int n) { return n < 42? g(n, 0, 1): throw std::out_of_range("too big"); } int main() { try { constexpr int f41 = f(41); // OK: constexpr int f43 = f

Is it well-defined to cast xvalues to lvalues for passing to functions?

落爺英雄遲暮 提交于 2019-12-21 03:32:54
问题 Recently I've discovered that sometimes being able to turn rvalues temporarily into lvalues can be useful for me. I've been using the following tool: #include <type_traits> template <typename T> inline constexpr std::remove_reference_t<T> &lvalue(T &&r) noexcept { return static_cast<std::remove_reference_t<T> &>(r); } It's useful when you have to use functions that require lvalues as arguments, but you don't have any interest in what those particular values get changed into. For when you are

Why isn't the “noexcept” specifier part of the function type?

南笙酒味 提交于 2019-12-21 03:18:31
问题 I don't get it why? I don't think compatibility should be a problem as functions declared without the specifier actually have it implicitly defined to false. If it's about name mangling - can we just suppose that old one (existing) will imply noexcept(false) and add another new symbol to the mangling for noexcept(true). This is going to be useful when working with templates as now comparing function type and noexcept specifier should be done seperatly. What I basically mean is this: int func(

Differences between std::is_integer and std::is_integral?

徘徊边缘 提交于 2019-12-21 03:13:38
问题 C++11 provides two type trait template classes: std::is_integer and std::is_integral . However, I cannot tell the differences between them. What type, say T, can make std::is_integer<T>::value true and make std::is_integral<T>::value false? 回答1: std::is_integer<T> does not exist. That being said, std::numeric_limits<T>::is_integer does exist. I'm not aware of any significant difference between std::numeric_limits<T>::is_integer and std::is_integral<T> . The latter was designed much later and

Is the behaviour of a program that has undefined behaviour on an unreachable path defined? [duplicate]

*爱你&永不变心* 提交于 2019-12-21 03:13:23
问题 This question already has answers here : Can code that will never be executed invoke undefined behavior? (9 answers) Closed last year . Consider void swap(int* a, int* b) { if (a != b){ *a = *a ^ *b; *b = *a ^ *b; *a = *a ^ *b; } } int main() { int a = 0; int b = 1; swap(&a, &b); // after this b is 0 and a is 1 return a > b ? 0 : a / b; } swap is an attempt to fool the compiler into not optimising out the program. Is the behaviour of this program defined? a / b is never reachable, but if it

Access to constexpr variable inside lambda expression without capturing

孤街醉人 提交于 2019-12-21 03:10:50
问题 In the following example, I can access the constexpr variable x from inside the lambda y without explicitly capturing it. This is not possible if x is not declared as constexpr . Are there special rules that apply to constexpr for capturing? int foo(auto l) { // OK constexpr auto x = l(); auto y = []{return x;}; return y(); // NOK // auto x2 = l(); // auto y2 = []{ return x2; }; // return y2(); } auto l2 = []{return 3;}; int main() { foo(l2); } 回答1: Are there special rules that apply to

Access to constexpr variable inside lambda expression without capturing

吃可爱长大的小学妹 提交于 2019-12-21 03:10:13
问题 In the following example, I can access the constexpr variable x from inside the lambda y without explicitly capturing it. This is not possible if x is not declared as constexpr . Are there special rules that apply to constexpr for capturing? int foo(auto l) { // OK constexpr auto x = l(); auto y = []{return x;}; return y(); // NOK // auto x2 = l(); // auto y2 = []{ return x2; }; // return y2(); } auto l2 = []{return 3;}; int main() { foo(l2); } 回答1: Are there special rules that apply to

Declaration and prototype difference

試著忘記壹切 提交于 2019-12-20 20:16:56
问题 What is the difference between declaration and prototype in C? In which situations they are called declarations and in which prototypes? 回答1: TL;DR; All prototypes are declarations, but not all declarations are prototypes. Declaration is the generic terminology used in the standards, prototype is more specific. Quoting C11 , chapter §6.7 A declaration specifies the interpretation and attributes of a set of identifiers. [...] and from §6.7.6, Each declarator declares one identifier, and