in Education by
Is the following code supposed to compile? #include void foo() { const std::pair x = {1, 2}; auto [a, b] = x; static_assert(std::is_const_v); static_assert(std::is_const_v); } MSVC says "yes!". GCC says "oh no, man!". Clang says "no way!". So, is this an MSVC bug? The standard is not straightforward here (I had a quick look), but considering the rules for auto, I suppose, a and b should be copied discarding cv-qualifier. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Is the following code supposed to compile? It is not. This is an MSVC bug. A structured binding declaration introduces a new name (for specification only), e, that is declared like: auto e = x; The type of e is called E, and since the initializer is tuple-like, the types of the bindings are given by tuple_element_t. In this case E is pair, so the two types are just int. The rule for decltype of a structured binding is to give the referenced type, so decltype(a) and decltype(b) are both int. The important part here is that a and b (the structured bindings) come from the invented variable (e), and not its initializer (x). e is not const because you just declared it auto. What we're doing is copying x, and then taking bindings into this (non-const) copy.

Related questions

0 votes
    Is the following code supposed to compile? #include void foo() { const std::pair x = {1, 2}; ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I'm trying to read data from scene.bin files using Microsoft::glTF SDK. TinyGLTF is not an option ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    I'm relatively new to C++, recently moved from C# and Java (and before that, used to work in ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Take the following code snippet: #include std::vector good; //illegal, because std::allocator is ill-formed ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    I'd like to ensure my RAII class is always allocated on the stack. How do I prevent a class from ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    I'd like to ensure my RAII class is always allocated on the stack. How do I prevent a class from ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    I have a multi-threaded program whereas each thread, at the start of execution, acquires a single connection ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 7, 2022 in Education by JackTerrance
0 votes
    I have a multi-threaded program whereas each thread, at the start of execution, acquires a single connection ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 6, 2022 in Education by JackTerrance
0 votes
    I have a multi-threaded program whereas each thread, at the start of execution, acquires a single connection ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 5, 2022 in Education by JackTerrance
0 votes
    I have a multi-threaded program whereas each thread, at the start of execution, acquires a single connection ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 5, 2022 in Education by JackTerrance
0 votes
    I have a multi-threaded program whereas each thread, at the start of execution, acquires a single connection ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 5, 2022 in Education by JackTerrance
0 votes
    I have a multi-threaded program whereas each thread, at the start of execution, acquires a single connection ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 5, 2022 in Education by JackTerrance
0 votes
    I am implementing a display algorithm where we can have multiple layers of windows based on their z-order i ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 28, 2022 in Education by JackTerrance
0 votes
    I am implementing a display algorithm where we can have multiple layers of windows based on their z-order i ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 27, 2022 in Education by JackTerrance
...