730
questions
-1
votes
0
answers
65
views
How am I getting the address of rvalue? [duplicate]
#include <bits/stdc++.h>
using namespace std;
void func(int *b, int &&a) {
// Printing the address of the rvalue reference
cout << &a << endl;
// Assigning ...
-1
votes
0
answers
39
views
Instantiating an object using an r-value in C++ [duplicate]
#include <iostream>
class MyClass {
private:
int num;
int* num_ptr;
public:
MyClass(); //default constructor
MyClass(const MyClass& source); //copy ...
1
vote
1
answer
101
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
97
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
90
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)
] ...
0
votes
0
answers
129
views
MSVC constexpr error "a non-constant (sub-)expression was encountered (with no info) (works on Clang!)
While working on my own array types, I encountered this issue, where one of my unit tests passes for Clang, but fails on MSVC with the following messages:
error C7595: 'UnitTest': call to immediate ...
2
votes
1
answer
69
views
Can invoke C++ methods on temporary r-values, but can't pass the same to global functions
In the following code sample, class A defines an operator% that takes a string as a class member; and there's also a global operator % that takes an A& and an int. In the main, I can instantiate ...
0
votes
0
answers
70
views
Understanding C++ Postfix Increment Operator Behavior in Assignment: Unexpected Results with obj1++ = obj2;
I'm currently reading the Deitel C++ book, and it mentioned the behavior of the postfix increment operator, stating that it typically returns a temporary object containing the original value before ...
0
votes
0
answers
64
views
why the rvalue is deduced to be lvalue? [duplicate]
The code is attach as follow. i tried to implement a method to check the expression or varible's value type. but it seems failed to check rvalue.
#include <iostream>
#include <type_traits>
...
1
vote
1
answer
110
views
Using rvalue in the condition statement of `for` loop in C++
I am working on a programming task that takes in an n value and uses three threads to print zero, even and odd numbers.
For example, if n = 3, it will print 010203. I encountered a strange behavior ...
5
votes
1
answer
139
views
How does std::forward work in the context of a fold expression?
In the MSVC STL, the implementation of std::apply is as follows:
template <class _Callable, _Tuple_like _Tuple, size_t... _Indices>
constexpr decltype(auto) _Apply_impl(_Callable&& _Obj, ...
0
votes
2
answers
116
views
Function Params as 'const T&' and 'T&&'
Need to understand and figure out how to work efficiently with these 2 definitions on lowlevel.
void func(const T& foo)
{
T bar = foo;
}
// Lets overload this
void func(T&& foo)
{
...
-2
votes
1
answer
96
views
cannot bind non-const lvalue reference of type 'Fraction&' to an rvalue of type 'Fraction'
I am trying to make a struct Fraction. I have defined constructors and operator>> and operator<< for it, and that works correctly.
When I was defining operator+ I got the following error ...
-2
votes
1
answer
64
views
How can an object find that is rvalue reference(unnamed value) in C++? [duplicate]
I have a class named Matrix. I want overload operator ! that return transpose of matrix.
When the main matrix is a unnamed object I prefer to use it's allocated memory to construct transpose matrix, ...
3
votes
1
answer
168
views
Why std::string a; std::string b; a + b = "abc"; OK? [duplicate]
#include <iostream>
#include <string>
int main()
{
std::string a;
std::string b;
a + b = "dadas";
}
PS D:\WeCode\local_c++> & 'c:\Users\z00841303.vscode\...
5
votes
1
answer
108
views
C++ Conditional expression removes reference when other type is throw expression [duplicate]
I have a function that takes a reference to a T*: void f(T *&t);. When I call it with a conditional expression with a throw, f(t == nullptr ? throw "nullptr" : t), the program fails to ...
1
vote
1
answer
144
views
Incrementing rvalue in C++ [duplicate]
Does anyone know why this code does not compile
int main() {
int i = 7;
int j = ++i++; // error: lvalue required as increment operand
}
while this code compiles perfectly?
int main() {
Int i{7};...
2
votes
2
answers
497
views
What does is_constructible_v<std::string&&, std::string&&> mean by?
I can understand what is is_constructible_v<std::string, std::string&&>.
But what does is_constructible_v<std::string&&, std::string&&> mean by?
What is difference ...
4
votes
1
answer
113
views
Why rvalue references behave differently based on types?
I'm reading about C++ rvalue references and value categories for expressions. However, I'm not able to grasp (from a lvalue, xvalue, prvalue point of view) why the following code works as expected:
...
3
votes
0
answers
24
views
How to correctly write separate methods for rvalue and lvalue overloads?
I have the following code.
#include <iostream>
#include <string>
struct A {
int x;
int y;
};
struct B {
A a;
~B(){
std::cout << "deleted" << ...
2
votes
1
answer
163
views
How to call rvalue methods of members?
I'm writing a class (Interface), which encapsulates a class (Impl) which happens to have an rvalue function (see Impl::Close).
class Impl
{
public:
/// Called from Dtor.
/// You can't reuse ...
0
votes
1
answer
46
views
I have a question depending on the location of 'const' in C++
int i = 0;
int* lv1 = &(++i); //++i is lvalue
//int* rv1 = &(i++); //i++ is rvalue
const int* &rv1 = &i;
int* const &rv2 = &i;
int* &&rv3 = &i;
const int &rv4 = ...
2
votes
1
answer
130
views
What are the rules of rvalue(&&) lvalue(&) reference binding in templates with regard to reference collapsing?
Consider these cases:
int i{};
int& ir{i};
class A{
public:
int& i;
A(int&& pi):i(pi){}
};
A a1{i}; // Error // case 1
A a2{int(1)}; // OK // case 2
class B{
public:
int&...
0
votes
3
answers
94
views
How to implement a make_rv function in c++
I want to write a template which can turn lvalue/rvalue to rvalue using universal reference like std::forward
For rvalue, just forward it.
For lvalue, copy it.
it is used as below
template<typename ...
2
votes
1
answer
100
views
Why the ownership of unique_prt is not transfered when using rvalue reference?
If I run the code
class Myc {
private:
int m{0};
public:
Myc(int ii) : m{ii} {};
~Myc() { std::cout << "Myc Destructed\n"; }
int getM() const { return m; }
};
...
1
vote
1
answer
58
views
An error occurred while constructing a 3D vector class
I'm debugging a 3D vector code, but I get the following error
/usr/bin/g++ -fdiagnostics-color=always -g /home/fangrui/vectorFEM/vectorFEM/main.cpp -o /home/fangrui/vectorFEM/vectorFEM/main
In file ...
1
vote
1
answer
58
views
Template specialization for rvalues
I have a compact set of functions that I use to write arbitrary data to a comma separated values file. It looks something like this:
template<typename T>
void log(std::ostream& out, T a) {
...
-2
votes
1
answer
127
views
What is faster to compute in C? (x==0) or (0==x)? [duplicate]
I wonder what is better to compute in C language:
if (x==0)
{
// Some code ...
}
of
if (0==x)
{
// Some code ...
}
I know the last is better in case the programmer forgets the second "...
0
votes
3
answers
86
views
How to do: non const member function used on objects left side and const member function used on objects right side of assignment
Consider the following C++ code:
class CBuffer
{
private:
char Array_L[10];
char Array_R[10];
public:
const char& AccessByte(int index) const { return Array_R[index]; }
char& ...
2
votes
1
answer
207
views
What does the "lvalue to rvalue" conversion mean for std::decay?
I am confused about the wording for std::decay on cppreference. They say it applies "lvalue to rvalue." I know std::decay removes referenceness, so is this what they mean by that? If so, why ...
0
votes
0
answers
123
views
Is there a way of preventing a C++ compiler from optimizing a copy of a temporary rvalue into a move operation?
eg:
std::vector<int> i;
i = std::vector{5, 4, 3, 2, 1}
In the above case, a compiler will typically optimize the second line into a move operation instead of a copy operation despite the lack ...
0
votes
1
answer
354
views
Non-member operator: cannot bind non-const lvalue reference to an rvalue
Binding a non-const rvalue to a rvalue with member operators work (case A), but binding to a non-member operator (case B) does not:
struct A
{
A & operator<<(int i) { return *this; }
};
...
3
votes
2
answers
132
views
range parameter for any iterable input with properly convertible elements
I am currently trying to get used to C++20 especially concepts and ranges in this case. Hopefully the title fits my problem I am not really sure yet what I ran into.
I want to create a set method for ...
4
votes
2
answers
226
views
Why doesn't make_pair<string, string>() call the copy contructor, when given const string&? [duplicate]
Both GCC and Clang refuse to compile this one:
#include <string>
#include <utility>
using namespace std;
int main() {
const string s = "12345";
const string& r = s;
...
2
votes
1
answer
45
views
How does the system know about the type of address it is pointing to in case of a rvalue?
I have some doubt (possibly a misconception) regarding the concept of rvalues when it comes to pre-increment or post-increment. Consider the following C program:
#include <stdio.h>
int main() {
...
3
votes
1
answer
209
views
Why is it ok to assign lvalue to rvalue in C++? [duplicate]
string s = "hello";
s + s = s; //compiles with no error/warnings
I was expecting a compilation error given rvalues are temporary values and can't be appear as an assigned value in an ...
1
vote
3
answers
248
views
c++ 20: how to find that the template argument is a rvalue reference
I want to pass a parameter to a function, do for, and if the argument is rvalue, then move objects instead of copying. How to do that?
template<class T> void func(T&& v){
for (auto&...
1
vote
1
answer
230
views
Move semantics with std::vector
I have a question concerning the move semantics in C++. See this example:
class Buck {
public:
Buck(std::vector<int> param) : data{param} {}
std::vector<int> data;
};
int main() {...
6
votes
1
answer
182
views
Ambiguous overload with different user cast operator for lvalue and rvalue cases
I try to compile this test code
struct MyData { /*..*/ };
template < typename T >
struct Wrapper
{
T m_value;
operator T const & () const & { return m_value; }
operator T &...
1
vote
4
answers
119
views
Why I can take address of *v.begin() where v is a std::vector
#include <vector>
#include <cstdio>
using namespace std;
int f()
{
int* a = new int(3);
return *a;
}
int main()
{
//printf("%p\n", &f());
vector<int> v{...
0
votes
1
answer
432
views
Why std::move doesn't avoid a second destruction?
I know ordinary std::vector::push_back() will copy the object. I hope this code would only destruct a only once, using std::move() and A(A&&) noexcept to avoid copying. But it doesn't seem to ...
1
vote
1
answer
42
views
Providing a template type of the same type as argument causes an error for rvalue reference function
I'm using a library with a templated function that takes an rvalue reference, and for clarity, I am explicitly stating the template type instead of relying inference. I noticed that when the template ...
2
votes
1
answer
249
views
rvalue reference -what is new with 'move constructor' that could not have been implemented in C++98?
I am new at C++ 11/17 and trying to understand how the rvalue reference move works.
In the code below what is the difference between the "modified copy constructor" and "move ...
0
votes
2
answers
211
views
Why do I get this lvalue & rvalue error while using a void function in C++? [duplicate]
I'm working on this exercise 18.7 in C++ Primer Plus (6th edition):
and the code I gave was like this, using the lambda expression as required:
#include <iostream>
#include <array>
#...
1
vote
0
answers
13
views
Forwarding rvalue into a thread where it is further forwarded to another function
I'm trying to create a deadline class which starts a thread in its constructor. Once the deadline (a time priod) occur within a thread, a function provided as argument should be executed.
The class'es ...
5
votes
1
answer
333
views
Is there a technical reason why range-based for loop doesn't detect whether it's looping on an rvalue?
The reason for the question is that I've seen code like this:
auto fun(std::vector<Foo>&& v) {
std::vector<Bar> w;
for (auto&& e : v /* not an rvalue, but keep ...
0
votes
2
answers
522
views
C++ class member rvalue assignment
I have seen C++ code that assigns a class member using an std::move call on an rvalue as follows:
class Widget {
std::vector<int> m_data{};
public:
// 1. OK
// x contents are copied ...
0
votes
1
answer
241
views
How to use an abstract class rvalue reference member?
I have an abstract class Base and derived class Derived:
class Base
{
public:
Base(int n) :_n(n) { _arr = new int[n]; }
virtual ~Base() { delete[] _arr; }
Base(Base&& other) { _n = ...
1
vote
1
answer
367
views
Return a shared pointer from a function vs Capturing a shared pointer in a Lambda
I am constructing a shared pointer in a function_1 and giving it as a capture to a lambda.
I think this is an issue, could you please confirm if this safe or I am right and I shoudn't be doing this?
#...
2
votes
1
answer
171
views
error use of deleted function when trying to pass rvalue to a tuple
Original context:
I am trying to pass a tuple of (object, expected_value_of_some_property) to a test function
I created a simple class to reproduce the error I am facing:
template <typename T>
...