language-lawyer

Why does 'typeid(x) == typeid(y)' evaluate to true, where 'x' and 'y' are id-expression of type T and T& respectively?

|▌冷眼眸甩不掉的悲伤 提交于 2020-06-25 18:03:33
问题 I am reading the C++11 draft standard and the section on [expr.typeid] mentions the following (emphasis mine): [...] When typeid is applied to an expression other than a glvalue of a polymorphic class type, the result refers to a std::type_info object representing the static type of the expression. Lvalue-to-rvalue (4.1), array-topointer (4.2), and function-to-pointer (4.3) conversions are not applied to the expression. If the type of the expression is a class type, the class shall be

Is an alias template considered equal to the “same” template? [duplicate]

瘦欲@ 提交于 2020-06-25 03:27:26
问题 This question already has answers here : Alias of a template. Who's right? (2 answers) Equality of template aliases (1 answer) Closed 2 months ago . The context This question asked if it is possible to write a trait that can retrieve the template from a given instantiation. In a nutshell the question was: "Is it possible to write a f such that f<foo<int>>::type is foo ?" This answer presents something that I would have expected to be exactly what that question asked for, but the answer

placement new on a class with reference field

本小妞迷上赌 提交于 2020-06-25 03:15:33
问题 This is a code example from the C++20 spec ([basic.life]/8): struct C { int i; void f(); const C& operator=( const C& ); }; const C& C::operator=( const C& other) { if ( this != &other ) { this->~C(); // lifetime of *this ends new (this) C(other); // new object of type C created f(); // well-defined } return *this; } int main() { C c1; C c2; c1 = c2; // well-defined c1.f(); // well-defined; c1 refers to a new object of type C } Would the following be legal or undefined behavior : struct C {

What is the memory layout of vector of arrays?

天涯浪子 提交于 2020-06-24 18:14:43
问题 can anybody explaine the memory layout of std::vector<std::array<int, 5>> vec(2) does it provide contiguous memory block of a 2D array with 2 rows of 5 elements? To my understanding, the vector of vectors std::vector<std::vector<int>> vec(2, std::vector<int>(5)) provide the memory layout of two contiguous arrays of length 5 element s in different locations in memory. Will it be the same for the vector of arrays? 回答1: Arrays do not have any indirection, but just store their data "directly".

Where are arguments positioned in the lexical environment?

偶尔善良 提交于 2020-06-24 12:52:42
问题 The following code always prints the argument passed in to parameter a , regardless of the presence of a variable with the same name. Presumably because parameter identifiers are bound separately to variables in scope. Where are they positioned? Are they in the lexical environment? function foo(a, b = () => a) { var a = 1 console.log(b()) } foo() // undefined foo(2) // 2 Is it that var declarations end up in the special VariableEnvironment, while parameters are positioned in the

Are static class members guaranteed to be initialized before `main` is called?

守給你的承諾、 提交于 2020-06-24 04:48:58
问题 Is there any guarantee that static class members are initialized before main is called? 回答1: I think no : [C++03: 3.6.2/3]: It is implementation-defined whether or not the dynamic initialization (8.5, 9.4, 12.1, 12.6.1) of an object of namespace scope is done before the first statement of main . If the initialization is deferred to some point in time after the first statement of main , it shall occur before the first use of any function or object defined in the same translation unit as the

Are static class members guaranteed to be initialized before `main` is called?

可紊 提交于 2020-06-24 04:48:28
问题 Is there any guarantee that static class members are initialized before main is called? 回答1: I think no : [C++03: 3.6.2/3]: It is implementation-defined whether or not the dynamic initialization (8.5, 9.4, 12.1, 12.6.1) of an object of namespace scope is done before the first statement of main . If the initialization is deferred to some point in time after the first statement of main , it shall occur before the first use of any function or object defined in the same translation unit as the

Are static class members guaranteed to be initialized before `main` is called?

最后都变了- 提交于 2020-06-24 04:47:59
问题 Is there any guarantee that static class members are initialized before main is called? 回答1: I think no : [C++03: 3.6.2/3]: It is implementation-defined whether or not the dynamic initialization (8.5, 9.4, 12.1, 12.6.1) of an object of namespace scope is done before the first statement of main . If the initialization is deferred to some point in time after the first statement of main , it shall occur before the first use of any function or object defined in the same translation unit as the

Why does 'A a{};' compile when the default constructor A::A() is deleted? [duplicate]

半城伤御伤魂 提交于 2020-06-23 07:41:21
问题 This question already has answers here : Deleted default constructor. Objects can still be created… sometimes (3 answers) When is a private constructor not a private constructor? (3 answers) Closed 2 years ago . Here's the code example in question: struct A { A() = delete; }; int main() { // A a(); // compiles, since it's a function declaration (most vexing parse) // A a; // does not compile, just as expected A a{}; // compiles, why? The default constructor is deleted. } Try it here with any

Dereferencing one past the end pointer to array type

♀尐吖头ヾ 提交于 2020-06-23 07:08:08
问题 Is it well defined in c++ to dereference a one-past-the-end pointer to an array type? Consider the following code : #include <cassert> #include <iterator> int main() { // An array of ints int my_array[] = { 1, 2, 3 }; // Pointer to the array using array_ptr_t = int(*)[3]; array_ptr_t my_array_ptr = &my_array; // Pointer one-past-the-end of the array array_ptr_t my_past_end = my_array_ptr + 1; // Is this valid? auto is_this_valid = *my_past_end; // Seems to yield one-past-the-end of my_array