All Questions
5,164
questions
3
votes
1
answer
59
views
What is the rationale for the expression class{}.refmem to be an lvalue expression
I came to know that S{}.rval is an lvalue expression. I want to know the rationale behind considering it an lvalue expression.
Why are access to non-static member of reference type lvalue, even when ...
-1
votes
0
answers
37
views
Lifetime of Temporary Passed to Multiple Functions by Const Reference in the Same Lexical Expression [duplicate]
According to the answers to this question, a temporary created in an expression lasts for the entire expression. This seems to imply that we can pass a temporary into and return it from functions by ...
0
votes
0
answers
63
views
Confusion regarding addresses and references in C++. What are the data types of addresses and references respectively? [duplicate]
I understood that the & operator is context dependent,
int var = 5;
int* ptr = &var;
In the above code, &var returns the address of var, then gives it to the pointer.
On the other hand
...
1
vote
1
answer
124
views
Is it possible to initialize a struct reference with an array?
Is it possible to do something like this?:
struct S {
int length;
char data[100];
};
class C {
uint8_t buffer[10];
S& s = buffer[0];
};
If I use pointer it compiles, but working with ...
0
votes
2
answers
97
views
Why this constant struct can be modified in this way?
Recently I encounter the following code, it is an excerpt from tutorial of open-source package AMReX text
Array4<Real> const& a = fab1.array();
Dim3 lo = lbound(a);
Dim3 hi = ubound(...
-3
votes
0
answers
106
views
Are pointer variables sometimes faster to initialize than reference variables?
Let's say just for acquiring the MEMORY-ADDRESS and VALUE of some variable. If the variable is "nested" in pointers like so:
int val = 10;
int *ptr1 = &val;
int *ptr2 = ptr1;
int *ptr3 =...
0
votes
0
answers
57
views
Why doesn't std::array<T,N>::operator[] return an rvalue reference for rvalue object? [duplicate]
std::array<T,N>::operator[] always returns an lvalue reference (const if needed), but what can possibly be the use of a line of code like this, which is valid C++?
std::array<int,1>{1}[0] =...
6
votes
1
answer
95
views
Returning const reference from lambda stored as std::function segfaults [duplicate]
Here is the code:
#include <vector>
#include <functional>
class TestClass {
public:
const std::vector<int> &getStuff() const {
return callback();
}
protected:
...
0
votes
0
answers
48
views
Why is it ok to return a reference to a function's default argument? [duplicate]
#include <iostream>
//class for testing**
class T
{
public:
void print() { std::cout << "hello\n"; }
~T() { std::cout << "~T\n";}
};
//function for ...
2
votes
1
answer
94
views
Capture bit-fields by reference in a lambda expression
According to cppreference, bit-fields can only be captured by copy: https://en.cppreference.com/w/cpp/language/lambda. Period.
At the same time, one can see certain scenarios, e.g.:
struct U {
int ...
0
votes
1
answer
66
views
std::vector not initialized in initializer list when using reference member [duplicate]
I'm trying to initialize the values vector with 25 values of 3 (for example) using the following code:
#include <vector>
#include <iostream>
class VecClass {
public:
VecClass(int v)
...
1
vote
1
answer
33
views
inconsistency between compilers when getting a `constinit` reference on a template parameter object
In a previous post I discussed a technic to create static values from non-type template parameters.
In this post and its accepted answer, a code was given to achieve this goal but it fails to compile ...
1
vote
4
answers
178
views
Is passing a reference to a primitive from C++ to C context undefined behaviour?
I'm wondering about the behaviour of passing references of an int or float from C++ to C. So the called function expects an int, but I call with an int& Is this defined?
The compiler will ...
0
votes
1
answer
101
views
Can't understand why copy constructor gets invoked
The following should be a MRE:
#include <iostream>
class Vector {
public:
double x, y, z;
// Default constructor
Vector() : x(0), y(0), z(0) {
std::cout << "...
4
votes
1
answer
166
views
Const-correctness of std::reference_wrapper
A const std::reference_wrapper works similar to a reference, or a T* const (demo):
#include <functional>
struct S {
void operator()() { i++; }
int i = 0;
};
int main() {
S s;
...