temporary-objects Questions
1
Solved
Section "15.6.2 Initializing bases and members" (N4713) has the following example following item 11:
struct A {
A() = default; // OK
A(int v) : v(v) { } // OK
const int& v = 42; // OK
};
A...
Fletcher asked 3/9, 2018 at 10:31
2
Edited 12 Feb
I've just recently come up with an odd crash using some SWIG-generated Python wrappers for some C++ classes. It seems that the combination of SWIG and Python together are somewhat ea...
Motoring asked 12/2, 2011 at 0:45
2
Solved
Thanks to some segmentation faults and warnings in valgrind, I found that this code is incorrect and has some sort of dangling reference in the for-range loop.
#include<numeric>
#inclu...
Poulard asked 20/7, 2018 at 6:17
1
Solved
The code at this GitHub file uses a C++ variable "declaration" syntax I'm not familiar with:
std::unique_ptr<CRecentFileList> {m_pRecentFileList} = std::make_unique<CRecentFileList>(.....
Dorcus asked 4/7, 2018 at 9:33
1
Solved
In C++ you can bind return value of a function (which return value, not reference) to const reference and code will still be valid because lifetime of this temporary will be prolonged till the end ...
Stereo asked 4/6, 2018 at 4:2
7
I have an existing function:
void foo(const Key* key = nullptr)
{
// uses the key
}
I want to pass it pointer to temporary Key object (i.e. rvalue) like:
foo(&Key());
This causes compila...
Johannejohannes asked 23/11, 2017 at 14:51
2
I ran into this while compiling some portable code in gcc. Basically this strange code compiles in Visual studio which really just blows my mind:
class Zebra {int x;};
Zebra goo() {Zebra z; return...
Octant asked 5/5, 2013 at 2:59
1
Solved
There is a subtle difference between the language used at cppreference.com and by the C++11 Standard regarding when the life of a temporary object is extended (emphasis mine).
From cppreference.co...
Token asked 26/5, 2017 at 19:46
2
Solved
consider the following example program:
#include <iostream>
using namespace std;
struct t
{
~t() {cout << "destroyed\n"; }
};
int main()
{
cout << "test\n";
t(), cout <<...
Diagnostics asked 1/6, 2017 at 13:52
1
Solved
I can't imagine this isn't already duplicate, but I can't easily find the answer since the more complex scenarios specifically to C++ seem to dominate the discussion0.
Is it legal to take take the...
Isocracy asked 29/1, 2017 at 23:38
5
Solved
The following code is producing the incorrect output:
string my_string="My_First_Text";
char * my_pointer=(char *)(my_string+"My_Second_Text").c_str();
Why? As I am initializing my_pointer, I pr...
Oriente asked 22/1, 2017 at 6:54
7
Solved
I've created a simple test case exhibiting a strange behavior I've noticed in a larger code base I'm working on. This test case is below. I'm relying on the STL Map's "[ ]" operator to create a poi...
Tko asked 25/10, 2010 at 19:2
2
Solved
In C++ (please correct me if wrong), a temporary bound via constant reference is supposed to outlive the expression it is bound to. I assumed the same was true in Rust, but I get two different beha...
Towery asked 27/10, 2016 at 16:6
4
Solved
In Eckel, Vol 1, pg:367
//: C08:ConstReturnValues.cpp
// Constant return by value
// Result cannot be used as an lvalue
class X {
int i;
public:
X(int ii = 0);
void modify();
};
X::X(int ii) {...
Tuckie asked 5/6, 2012 at 13:1
2
Solved
Why did the C++ committee decide that const references should extend the lifetime of temporaries?
This fact has already been discussed extensively online, including here on stackoverflow. The defi...
Bordie asked 27/9, 2016 at 7:11
3
Solved
I'm implementing a decorator pattern on immutable objects with the pointer-to-implementation idiom. Basically my setup looks like this
struct Object : ObjectBase {
void doSmth() override {
impl-...
Hulbard asked 8/6, 2016 at 18:11
1
Solved
It is not clear to me whether the lifetime of a temporary object would be extended by binding it to a const reference in a ?: expression:
class Foo {...};
Foo *someLValue = ...;
const Foo& =...
Racon asked 17/5, 2016 at 3:57
3
Solved
I know that const reference prolongs the life of a temporary locally. Now I am asking myself if this propriety can be extended on a chain of temporary objects, that is, if I can safely define:
std...
Legging asked 2/5, 2016 at 12:35
4
Solved
I'm somewhat confused about the declaration of functions returning const references to temporaries.
In the following code
#include <string>
#include <iostream>
using namespace ...
Fruity asked 17/4, 2016 at 6:33
5
Solved
The following code prints one,two, three. Is that desired and true for all C++ compilers?
#include <iostream>
struct Foo
{
const char* m_name;
~Foo() { std::cout << m_name << ...
Naominaor asked 19/2, 2010 at 18:51
3
Solved
I would like to iterate over a temporary valarray, but it isn't working. Here is my (non-working) code:
#include <iostream>
#include <valarray>
int main()
{
using namespace std;
vala...
Condyloma asked 28/1, 2016 at 16:39
2
Solved
1) Is it undefined behavior to return a reference to a temporary, even if that reference is not used? For example, is this program guaranteed to output "good":
int& func()
{
int i = 5;
retur...
Toilsome asked 18/11, 2015 at 20:49
4
Solved
Consider the following code in C++:
struct A {A(int);};
A foo() {return static_cast<A>(0);}
A x = foo();
Here static_cast<A>(0) creates a temporary object by the standard [5.2.9-4], ...
Bred asked 30/10, 2015 at 10:4
1
Solved
Given the following struct:
struct ABC
{
ABC(){cout << "ABC" << endl;}
~ABC() noexcept {cout << "~ABC" << endl;}
ABC(ABC const&) {cout << "copy" << endl;...
Maisiemaison asked 5/5, 2015 at 14:46
3
A regular string string literal has the following definition:
Ordinary string literals and UTF-8 string literals are also referred
to as narrow string literals. A narrow string literal has type...
Levigate asked 26/3, 2015 at 12:23
© 2022 - 2024 — McMap. All rights reserved.