Hacker News new | ask | show | jobs
by jcelerier 3002 days ago
> 1. I thought the cast to an rvalue reference would make overloads taking rvalue references be chosen during overload resolution. Since this is deterministic I thought whether something is moved or not would be quite guaranteed

well, take for instance this function :

    my_struct eat_pointer(std::unique_ptr<int>&& x) {
      if(x && *x > 0)
        return my_struct{std::move(x)};
      return my_struct{};
    }

    auto my_pointer = std::make_unique<int>(-456);
    auto res = eat_pointer(std::move(my_pointer));
    // at this point my_pointer hasn't moved a bit
    
sure, this code sucks, but that's always a possibility
1 comments

Thanks for the example, I understand. I missunderstood the poster above. I've come across quite a few coworkers who believe that move is something that happens on the compilers whim as part of some optimization and incorrectly assumed the same of the post i replied to.