Is it useful to move const objects?


virtual software company

I realize that the common sense "can't move const objects" isn't entirely true. If you declare the move ctor as, you can

X(const X&&);

The complete example is as follows:

#include <iostream>

struct X
{
    X() = default;
    X(const X&&) {std::cout << "const move\n";}
};

int main()
{
    const X x{};
    X y{std::move(x)};
}

Live on Coliru

Question: Why would anyone want such a thing? Any useful/workable scenarios?

track light race

Nothing has changed from your example. Yes, you wrote std::movean rvalue and called the move constructor, but didn't actually end up getting the move. No, because the object is const.

You won't be able to make any "moves" unless you've marked the members you're interested in . So there is no useful or even possible case.mutable

Related


Is it useful to move const objects?

virtual software company I realize that the common sense "can't move const objects" isn't entirely true. If you declare the move ctor as, you can X(const X&&); The complete example is as follows: #include <iostream> struct X { X() = default; X(const

Is it useful to move const objects?

virtual software company I realize that the common sense "can't move const objects" isn't entirely true. If you declare the move ctor as, you can X(const X&&); The complete example is as follows: #include <iostream> struct X { X() = default; X(const

Is it useful to move const objects?

virtual software company I realize that the common sense "can't move const objects" isn't entirely true. If you declare the move ctor as, you can X(const X&&); The complete example is as follows: #include <iostream> struct X { X() = default; X(const

Is it useful to move const objects?

virtual software company I realize that the common sense "can't move const objects" isn't entirely true. If you declare the move ctor as, you can X(const X&&); The complete example is as follows: #include <iostream> struct X { X() = default; X(const

Is it useful to move const objects?

virtual software company I realize that the common sense "can't move const objects" isn't entirely true. If you declare the move ctor as, you can X(const X&&); The complete example is as follows: #include <iostream> struct X { X() = default; X(const

Move semantics with const objects

gap I have code like this: class Pair{ public: Pair(Pair && other){}; Pair(Pair & other){}; }; class IROList{ public: virtual const Pair get(const char *key) const = 0; inline const Pair operator[](const char *key) const{ return this

Move semantics with const objects

gap I have code like this: class Pair{ public: Pair(Pair && other){}; Pair(Pair & other){}; }; class IROList{ public: virtual const Pair get(const char *key) const = 0; inline const Pair operator[](const char *key) const{ return this

Move semantics with const objects

gap I have code like this: class Pair{ public: Pair(Pair && other){}; Pair(Pair & other){}; }; class IROList{ public: virtual const Pair get(const char *key) const = 0; inline const Pair operator[](const char *key) const{ return this

Why can we use `std::move` on const objects?

Camino: In C++11, we can write the following code: struct Cat { Cat(){} }; const Cat cat; std::move(cat); //this is valid in C++11 When i call it std::movemeans i want to move the object i.e i will change the object. Moving constobjects is unreasonable, s

Why can we use `std::move` on const objects?

Camino: In C++11, we can write the following code: struct Cat { Cat(){} }; const Cat cat; std::move(cat); //this is valid in C++11 When i call it std::movemeans i want to move the object i.e i will change the object. Moving constobjects is unreasonable, s

Why can we use `std::move` on const objects?

Camino: In C++11, we can write the following code: struct Cat { Cat(){} }; const Cat cat; std::move(cat); //this is valid in C++11 When i call it std::movemeans i want to move the object i.e i will change the object. Moving constobjects is unreasonable, s

Is it useful to declare a reference type as const?

Naval Why would anyone do this: const object useless = null; const IEnumerable meaningless = null; Eric Lippert said that functionality is not implemented by default, and every possibility adds effort in testing, maintenance, etc. Why use the null value of a

Is it useful to declare a reference type as const?

Naval Why would anyone do this: const object useless = null; const IEnumerable meaningless = null; Eric Lippert said that functionality is not implemented by default, and every possibility adds effort in testing, maintenance, etc. Why use the null value of a

Matlab: faster? Are useful objects predefined?

Wuppertinger I have to do calculations in matlab where the matrix is very large. I've made sure to use matrix operations etc where possible. Now try fine-tuning. So let A, B, C and D be matrices: C=A*B; D=cos(C); It seems that the following approach would be

Matlab: faster? Are useful objects predefined?

Wuppertinger I have to do calculations in matlab where the matrix is very large. I've made sure to use matrix operations etc where possible. Now try fine-tuning. So let A, B, C and D be matrices: C=A*B; D=cos(C); It seems that the following approach would be

Matlab: faster? Are useful objects predefined?

Wuppertinger I have to do calculations in matlab where the matrix is very large. I've made sure to use matrix operations etc where possible. Now try fine-tuning. So let A, B, C and D be matrices: C=A*B; D=cos(C); It seems that the following approach would be

Matlab: faster? Are useful objects predefined?

Wuppertinger I have to do calculations in matlab where the matrix is very large. I've made sure to use matrix operations etc where possible. Now try fine-tuning. So let A, B, C and D be matrices: C=A*B; D=cos(C); It seems that the following approach would be

Is the [Ref] attribute of a const record parameter useful?

username With the latest Delphi version (Berlin/10.1/24), is the [Ref] attribute really necessary? I'm asking this because the online documentation says: Constant parameters can be passed to functions by value or by reference, depending on the specific compile

Is the [Ref] attribute of a const record parameter useful?

username With the latest Delphi version (Berlin/10.1/24), is the [Ref] attribute really necessary? I'm asking this because the online documentation says: Constant parameters can be passed to functions by value or by reference, depending on the specific compile

Is the [Ref] attribute of a const record parameter useful?

username With the latest Delphi version (Berlin/10.1/24), is the [Ref] attribute really necessary? I'm asking this because the online documentation says: Constant parameters can be passed to functions by value or by reference, depending on the specific compile

Is the [Ref] attribute of a const record parameter useful?

username With the latest Delphi version (Berlin/10.1/24), is the [Ref] attribute really necessary? I'm asking this because the online documentation says: Constant parameters can be passed to functions by value or by reference, depending on the specific compile

Is the [Ref] attribute of a const record parameter useful?

username With the latest Delphi version (Berlin/10.1/24), is the [Ref] attribute really necessary? I'm asking this because the online documentation says: Constant parameters can be passed to functions by value or by reference, depending on the specific compile

Move semantics and const references

Finn My class has string variables and I want to initialize them with the values passed to the constructor. My teacher thought we could pass strings as const references: MyClass::MyClass(const std::string &title){ this->title = title } But Clang-Tidy recomm

Move semantics and const references

Finn My class has string variables and I want to initialize them with the values passed to the constructor. My teacher thought we could pass strings as const references: MyClass::MyClass(const std::string &title){ this->title = title } But Clang-Tidy recomm

Move semantics and const references

Finn My class has string variables and I want to initialize them with the values passed to the constructor. My teacher thought we could pass strings as const references: MyClass::MyClass(const std::string &title){ this->title = title } But Clang-Tidy recomm

Move semantics and const references

Finn My class has string variables and I want to initialize them with the values passed to the constructor. My teacher thought we could pass strings as const references: MyClass::MyClass(const std::string &title){ this->title = title } But Clang-Tidy recomm

Move semantics and const references

Finn My class has string variables and I want to initialize them with the values passed to the constructor. My teacher thought we could pass strings as const references: MyClass::MyClass(const std::string &title){ this->title = title } But Clang-Tidy recomm

Convert Antlr syntax trees to useful objects

Richard Walton: I'm currently thinking about how best to take the AST generated with Antlr and turn it into a useful object that can be used in my program. The purpose of my grammar (besides learning) is to create an executable (runtime interpreted) language.