temporary-objects Questions

3

Solved

Is below code standard-correct? (godbolt) I.e. by-ref capturing a forwarding reference that represents a temporary, and returning the resulting lambda by-value from the function, within the same e...
Munition asked 7/4, 2019 at 13:4

4

Solved

The current C++ standard draft in [class.temporary]/7 contains the phrase a temporary object other than a function parameter object I was under the impression that function parameter objects are ...
Onanism asked 17/12, 2023 at 22:16

2

Solved

Consider the following code: (https://godbolt.org/z/8W699x6q6) int* p; const int*&& r = static_cast<int*&&>(p); Note: const int*&& is an rvalue reference to a pointer...

2

Solved

Consider the following code: void foo() { int arr[1]; *arr; // OK using T = int[1]; *T{}; // OK for Clang and MSVC // GCC error: taking address of temporary array } See live code at Compiler...
Sievers asked 6/9, 2023 at 10:19

1

Solved

Is the C++ code below well-formed? Will the std::string get destroyed before or after the function finishes executing? void my_function(const char*); ... my_function(std::string("Something&q...
Lombroso asked 28/7, 2023 at 0:17

2

Solved

Hi stackoverflow community, I'm a few months into C++ and recently I've been trying to grasp the concepts revolving around the "new" value categories, move semantics, and especially tempo...
Bradawl asked 4/3, 2021 at 11:56

6

Solved

I am using an STL vector that is a vector of Parameters. std::vector<Parameter> foo; I was trying to find a way to add Parameter objects to the vector without doing this: Parameter a; foo...
Marcus asked 19/6, 2013 at 14:36

1

I'm trying to understand lifetime extension guarantees in C++. Can someone explain why the usage of different types of parentheses below yields differing results in terms of when the temporar...
Lonna asked 24/4, 2023 at 2:11

1

Solved

I used the following syntactic sugar: for (auto& numberString: {"one", "two", "three", "four"}) { /* ... */} Is this valid code? AFAIK, based on this qu...
Moorehead asked 7/3, 2023 at 7:56

2

As a follow-up to this question, clang accepts the code provided there. This question has the following code: constexpr int func(int const& rf){ return rf; } int main(){ constexpr int value ...

2

This is a followup to my previous question, where the apparent consensus was that the change in treatment of cv-qualifications of prvalues was just a fairly minor and inconsequential change intende...
Lowboy asked 26/1, 2019 at 19:7

4

Solved

If we want to initialize an reference with an different type, we need to make it const (const type*) so that an temporary can be generated implicit and the reference binded to with. Alternativaly, ...
Duckling asked 17/7, 2022 at 20:29

3

Solved

I thought references only extend the lifetime of temporaries to the lifetime of the reference itself, but the output of the following snippet seems contradictory: #include <iostream> struct...
Jaques asked 23/9, 2012 at 17:38

1

Solved

Given a matrix template class mat<M,N,T> the following member function allows me to efficiently transpose a row vector or a column vector, since they have the same/corresponding memory footpr...
Tillett asked 29/7, 2021 at 1:1

1

Solved

A friend of mine showed me a program in C++20: #include <iostream> struct A { A() {std::cout << "A()\n";} ~A() {std::cout << "~A()\n";} }; struct B { cons...

6

Solved

Why does this: #include <string> #include <iostream> using namespace std; class Sandbox { public: Sandbox(const string& n) : member(n) {} const string& member; }; int main(...

3

Solved

Consider the following program: #include <iostream> int const * f(int const &i) { return &i; } int main() { std::cout << f(42); // #1 std::cout << f(42); // #2 ...
Englut asked 17/12, 2020 at 10:15

0

CWG 1815 asked (with minor edits): struct A {}; struct B { A&& a = A{}; }; B b1; // #1 B b2{A{}}; // #2 B b3{}; // #3 [...] #2 is aggregate initialization, which binds B::a to the tempora...
Cassidy asked 1/1, 2021 at 9:59

4

Solved

In the following simple example, why can't ref2 be bound to the result of min(x,y+1)? #include <cstdio> template< typename T > const T& min(const T& a, const T& b){ return ...
Thereinafter asked 8/4, 2019 at 7:9

2

Solved

According to the C++ standard you cannot bind a temporary to a non-const reference. Since the stream output operator is defined as template <class CharT, class Traits, class Allocator> std:...
Carbo asked 10/9, 2019 at 9:21

2

Solved

What's the difference in practice between LVALUE and RVALUE in the following code when I pass the text? I mean, in this specific case of a string (where the string is a string literal), is there an...
Poltroon asked 10/4, 2019 at 18:56

5

Preface Below code results in undefined behaviour, if used as is: vector<int> vi; ... vi.push_back(1); // thread-1 ... vi.pop(); // thread-2 Traditional approach is to fix it with std::mutex...
Krona asked 20/2, 2019 at 7:56

2

Solved

Is this code UB? struct A { void nonconst() {} }; const A& a = A{}; const_cast<A&>(a).nonconst(); In other words, is the (temporary) object originally const? I've looked through ...

3

Solved

Consider the following code - #include <iostream> #include <stdio.h> const int & retRef() { return 6; } int main() { const int& k = retRef(); printf("Value: %d\n", k); pr...
Pneumatics asked 3/12, 2018 at 6:50

2

Solved

I am trying to understand the interaction of perfect forwarding and constructors. My example is the following: #include <utility> #include <iostream> template<typename A, typename...
Barracks asked 25/9, 2018 at 12:50

© 2022 - 2024 — McMap. All rights reserved.