0

Consider the code below:

#include <string>
#include <iostream>

int main()
{
    std::string v = "abc";

    auto func = [v] () // mutable makes v move
    {
        static_assert(!std::is_const_v<decltype(v)>);
    
        static_assert(std::is_same_v<decltype(v), std::string>);
    
        //it does not move
        auto a = std::move(v);

        std::cout << "v: " << v;
    };

    func();

    return 0;
}

the output:

v: abc

Why v does not move?

As far, as I can guess, without mutable keyword something should be const-qualified. Where is this const?

Dmitriano
  • 1,878
  • 13
  • 29
  • 1
    Your test is unreliable because after move construction the moved-from instance is in an unspecified state. It isn't necessarily empty. For example if SSO is being used move construction might just be equivalent to copy construction. – François Andrieux Aug 02 '22 at 15:59
  • *Where is this const?* The const is implicit. The `mutable` negates the implicit const. – Eljay Aug 03 '22 at 17:23

0 Answers0