740 questions
- Bountied 0
- Unanswered
- Frequent
- Score
- Trending
- Week
- Month
- Unanswered (my tags)
3
votes
3
answers
200
views
Is a function call returning a pointer a prvalue?
This question arouse from the question "why is const in function return type, which is a const pointer, ignored?" (const pointer, not pointer to const)
int* const Foo();
is identical to
int*...
5
votes
2
answers
318
views
Can an rvalue of a specific class type be automatically cast to an lvalue of itself?
In C++, a function with signature void f(A& a) (non-template) only binds to a lvalue.
Is it possible for an rvalue to automatically cast itself to an lvalue?
This is what I tried:
#include <...
4
votes
1
answer
117
views
C struct assignment by unnamed struct initialized by member-wise list, syntax candy or unintentional memory overhead?
Prior statement: I'm a programmer long coding in C++ and Python, so kindly pardon and teach if my question looks too silly due to my thinking of C in an OOD way. Also I've tried lot searching on ...
PkDrew's user avatar
- 2,301
1
vote
1
answer
284
views
Is it OK for a function to return a local reference by value?
The code below appears to work just fine since the function returns by value:
// return by value
std::string getFoo() {
const std::string& myStr = getBar();
return myStr;
}
std::string ...
-2
votes
1
answer
176
views
Given an API that returns const char*, how to get its return value's address?
I have access to an API that returns const char *, where it is guaranteed that the underlying string is valid all through the program, like below:
extern const char *getStr(); // an API that returns ...
2
votes
2
answers
141
views
Value categories in C++: What does "can be moved from" mean?
In C++, the value category of an expression is determined by two independent properties:
Whether the expression has an identity
Whether the expression can be moved from
(References: cppreference and ...
2
votes
1
answer
118
views
Why are there no rvalue ref-qualified begin() overloads in STL containers?
In the following code:
#include <vector>
int main() {
using vec_t = std::vector<int>;
vec_t v;
auto it1{std::move(v).begin()};// type is vec_t::iterator
auto it2{std::...
phinz's user avatar
- 1,739
3
votes
1
answer
137
views
Why does adding std::move bring the capture back to the stack?
Given the following code that tries to allocate a lambda capture on the heap:
#include <array>
int test(void) {
//auto big_lambda = new auto([d = std::array<int, 1024>{5,1,2}]() { ...
4
votes
1
answer
109
views
Cannot return a named rvalue reference in a function with return type of lvalue reference?
In C++, a named rvalue reference is lvalue.
Why the below code yields "error: cannot bind non-const lvalue reference of type 'int&' to an rvalue of type 'int'"?
int & unmove(int &...
1
vote
2
answers
158
views
Are compound literals always lvalue?
I was reading C Notes for Professionals, in which, it claims that Compound Literals can only be lvalues. But LLMs have mixed answers upon that stating it could depend upon the context and could be ...
2
votes
1
answer
107
views
Why does passing rvalue to lvalue reference work in this case?
I was doing Leetcode when I stumbled upon this interesting technique. Here it is in short:
#include <iostream>
#include <vector>
void print(std::vector<int>& vec)
{
for (int ...
1
vote
2
answers
115
views
Designing function API to avoid object lifetime problems
I've got a function that returns a lambda.
auto make_scanner(std::vector<int> const &v) {
auto begin = v.cbegin();
auto end = v.cend();
return [begin, end] () mutable -> int {
...
1
vote
1
answer
147
views
move assignment argument throws error when dereferenced but works when member accessed directly
In the minimum needed code snippet, in the move assignment, why is the commented line *arg = nullptr; illegal and arg.p = nullptr; okay? If I understand correctly, both are modifying rvalue, yet on ...
5
votes
1
answer
114
views
May object pointer be always compared with nullptr in a constant expression?
The following program was reduced to demonstrate the question. struct A has a constexpr member function that compares this against some pointer, which can be nullptr. Then this member function is ...
0
votes
0
answers
100
views
Does the C++ standard library have function types that are non-copy-constructible?
I want to pass a bunch of lambdas around without having to make copies.
This is the declaration of one of the lambdas
[
promise = std::move(promise),
namedSeries = std::move(namedSeries)
] ...