language-lawyer

Different Pointer Arithmetic Results when Taking Address of Array

你离开我真会死。 提交于 2019-12-18 10:58:26
问题 Program: #include<stdio.h> int main(void) { int x[4]; printf("%p\n", x); printf("%p\n", x + 1); printf("%p\n", &x); printf("%p\n", &x + 1); } Output: $ ./a.out 0xbff93510 0xbff93514 0xbff93510 0xbff93520 $ I expect that the following is the output of the above program. For example: x // 0x100 x+1 // 0x104 Because x is an integer array &x // 0x100 Address of array &x+1 // 0x104 But the output of the last statement is different from whast I expected. &x is also the address of the array. So

Could a C++ implementation, in theory, parallelise the evaluation of two function arguments?

左心房为你撑大大i 提交于 2019-12-18 10:18:02
问题 Given the following function call: f(g(), h()) since the order of evaluation of function arguments is unspecified (still the case in C++11 as far as I'm aware), could an implementation theoretically execute g() and h() in parallel? Such a parallelisation could only kick in were g and h known to be fairly trivial (in the most obvious case, accessing only data local to their bodies) so as not to introduce concurrency issues but, beyond that restriction I can't see anything to prohibit it. So,

Does SFINAE apply to function bodies?

帅比萌擦擦* 提交于 2019-12-18 08:58:35
问题 I have the following sample code: class Serializable {}; class MyData : public Serializable {}; void GetData( Serializable& ) {} template<typename T> void GetData( T& data ) { std::istringstream s{"test"}; s >> data; } int main() { MyData d; GetData(d); } (Live Sample) Based on overload resolution rules, the non-template version should be preferred because the base class is of type Serializable . However, I expect SFINAE to kick in when there are errors in the template version when it is

Unqualified name lookup: Why local declaration hides declaration from using directive

a 夏天 提交于 2019-12-18 08:31:22
问题 Consider this code: namespace A { int i = 24; } namespace B { using namespace A; int i = 11; int k = i; // finds B::i, no ambiguity } And basic.lookup.unqual.2: §6.4.1 Unqualified name lookup [basic.lookup.unqual] The declarations from the namespace nominated by a using-directive become visible in a namespace enclosing the using-directive; see [namespace.udir]. For the purpose of the unqualified name lookup rules described in [basic.lookup.unqual], the declarations from the namespace

Expanding parameter pack into lambda with fold expression - gcc vs clang

爷,独闯天下 提交于 2019-12-18 08:27:43
问题 Considering the following code snippet: template <typename TF> void post(TF){ } template <typename... TFs> struct funcs : TFs... { funcs(TFs... fs) : TFs{fs}... { } void call() { (post([&]{ static_cast<TFs&>(*this)(); }), ...); } }; clang++ 3.8+ successfully compiles the code. g++ 7.0 fails to compile with the following error: prog.cc: In lambda function: prog.cc:10:43: error: parameter packs not expanded with '...': (post([&]{ static_cast<TFs&>(*this)(); }), ...); ~~~~~~~~~~~~~~~~~~~~~~~~^~

Is it undefined behavior to take the address of an uninitialized pointer?

徘徊边缘 提交于 2019-12-18 08:25:20
问题 N1570 states that this is undefined behavior: §J.2/1 The value of an object with automatic storage duration is used while it is indeterminate (6.2.4, 6.7.9, 6.8). And in this case, our pointer has an indeterminate value: §6.7.9/10 If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static or thread storage duration is not initialized explicitly, then: — if it has pointer type, it is initialized to a null pointer; I

Why isn't this rvalue promoted to an lvalue as specified in the reference?

℡╲_俬逩灬. 提交于 2019-12-18 08:16:40
问题 The Rust Reference says: The left operand of an assignment or compound-assignment expression is an lvalue context, as is the single operand of a unary borrow. [...] When an rvalue is used in an lvalue context, a temporary un-named lvalue is created and used instead. This rvalue promotion obviously works with borrowing: let ref_to_i32 = &27; // a temporary i32 variable with value 27 is created But it doesn't seem to work in an assignment (although the reference speaks about all lvalue contexts

Why isn't this rvalue promoted to an lvalue as specified in the reference?

冷暖自知 提交于 2019-12-18 08:16:07
问题 The Rust Reference says: The left operand of an assignment or compound-assignment expression is an lvalue context, as is the single operand of a unary borrow. [...] When an rvalue is used in an lvalue context, a temporary un-named lvalue is created and used instead. This rvalue promotion obviously works with borrowing: let ref_to_i32 = &27; // a temporary i32 variable with value 27 is created But it doesn't seem to work in an assignment (although the reference speaks about all lvalue contexts

Access to protected constructor of base class

百般思念 提交于 2019-12-18 07:40:46
问题 A derived class can call a protected base class constructor in its ctor-initializer , but only for its own base class subobject, and not elsewhere: class Base { protected: Base() {} }; class Derived : Base { Base b; public: Derived(): Base(), // OK b() { // error Base b2; // error } }; What does the standard say about this? Here is [class.protected]/1: An additional access check beyond those described earlier in Clause 11 is applied when a non-static data member or non-static member function

Does C++17 forbid copy elision in a case where C++14 allowed it?

浪子不回头ぞ 提交于 2019-12-18 07:35:41
问题 Consider the following: struct X { X() {} X(X&&) { puts("move"); } }; X x = X(); In C++14, the move could be elided despite the fact that the move constructor has side effects thanks to [class.copy]/31, This elision of copy/move operations ... is permitted in the following circumstances ... when a temporary class object that has not been bound to a reference (12.2) would be copied/moved to a class object with the same cv-unqualified type In C++17 this bullet was removed. Instead the move is