306 questions
2
votes
2
answers
159
views
Do C++ value categories/copy elision rules change with C++26 replaceable_if_eligible?
How does the concept of trivial relocatability from C++26 change how we reason about value categories?
Do they interact at all (e.g., temporary-materialization rules)?
For instance, if I have a ...
Best practices
1
vote
17
replies
6k
views
C++ use of std::move for return of rvalue function parameter
NRVO/RVO rules allow/require copy elision if a (non-conditional) return value is constructed by the called function - it is to be constructed on the caller's stack frame return value location (though ...
Advice
0
votes
7
replies
134
views
Do C++ value categories rules such as copy elision change with C++26 trivially_relocatable_if_eligible?
C++11 value categories (lvalues, xvalues, and prvalues) are deeply entangled to the core of C++ itself in a way that's not possible to reason about C++ w/o reasoning about value semantics.
C++17 ...
5
votes
0
answers
177
views
When is the ternary operator suitable for copy elision? [duplicate]
According to
this article
by Raymond Chen, ternary expression is not a copy elision candidate.
The problem with the ternary is that the ternary expression is not a copy elision candidate. The rule ...
7
votes
1
answer
339
views
Is visual studio standard conformant when it seemingly doesn't elide a copy when calling a function
Look at this program (godbolt):
#include <cstdio>
struct Foo {
~Foo() {
printf("~Foo\n");
}
};
void foo(Foo) {
}
int main() {
foo(Foo());
}
Based on my previous ...
3
votes
2
answers
59
views
Understanding conversion sequence and role of copy elision
I'd like to understand copy elision rules (if these are the one that apply) in a conversion sequence, during an object direct initialization.
#include <cstdio>
class To {
public:
To() { ...
1
vote
1
answer
106
views
C++ copy elision when returning a function argument
I'm puzzled as to how this code compiles: (https://godbolt.org/z/hjY4ca4rr)
class A
{
public:
A() = default;
A(A &&other) = default;
int x = 0;
};
A f(A a)
{
a.x = 1;
...
0
votes
0
answers
66
views
Utilize copy/move elision with a never again used reference
This has been bothering me for some time. Say we have a constructor that takes many parameters (copies or rvalues):
Object(A a, B b, C c, D d, ...);
When you need to use this constructor you can do ...
0
votes
0
answers
143
views
C++ NRVO in the presence of multiple return points
I have the following C++ code that returns a std::tuple<std::vector<int>, std::vector<int>>. My question is about copy elision and named return value optimisation. As I understand, ...
0
votes
1
answer
154
views
Why Does Disabling Non-Mandatory Copy Elision Result in Different Behaviors Before and After C++17
I've been experimenting with the following C++ code using different compiler flags and versions, observing differing behaviors in object construction between C++11 and C++17.
I noticed that with the -...
1
vote
1
answer
114
views
Copy elision when function returns a local variable and temporary via different paths
I have a class Foo
class Foo {
public:
Foo(std::string s) : s_(std::move(s)) {
std::cout << "Constructor " << s_ << "\n";
}
~Foo() {
...
0
votes
0
answers
63
views
How the mechanism of NRVO works in C++ function return values?
According to some sources, Named Return Value Optimization (NRVO) is performed when a return object; statement in a function is executed.
Nevertheless, the execution of
int foo(int a_func) {
std::...
3
votes
1
answer
117
views
Copy elision (e.g. NRVO) during constant evaluations
According to cppreference
In constant expression and constant initialization, copy elision is never performed.
Still it looks that it is not always so on practice. In the following program, function ...
15
votes
3
answers
996
views
Is copy elision in the form of named return value optimization permitted in C?
Is the following C program guaranteed to exit with 0 or is the compiler allowed to identify the objects s and t with one another as is permitted in C++ as the so-called named return value optimization ...
4
votes
1
answer
136
views
Why does operator() copy movable temporaries in Clang?
In the following C++23 program
struct A {
A() {}
A(A&&) = default;
void f(this A) {}
void operator() (this A) {}
};
int main() {
A{}.f(); // ok
A{}(); // Clang error
...