I have a class K
and I am constructing an object in a call to function test
. So I believe that the constructed K
is called an r-value. (Is that true?)
But I am puzzled, and bothered that the K
object is apparently const, not mutable. I don't want it to be.
Simple test program:
#include <iostream>
class K {};
std::string test (K &k) {return "mutable";}
std::string test (const K &k) {return "const";}
int main (int argc, const char **argv) {
std::cerr << "K constructed for function argument is " << test(K{}) << "\n";
K k;
std::cerr << "K constructed for local variable is " << test(k) << "\n";
}
Output:
K constructed for function argument is const
K constructed for local variable is mutable
Note that when I create the K
on the fly for passing as function argument I get a const object, whereas when I create it as a local variable I get a mutable.
For my purposes I really want a mutable for both cases. Can you tell me how to do that, or else convince me why I shouldn't?