Coffeehouse Post

Single Post Permalink

View Thread: is managed code faster than native?
  • User profile image
    Sven Groot

    , SteveRichter wrote


    In my C++ code all strings are in std::wstring and I am making a lot of use of unique_ptr<class>. And since I do a lot of function calls which return these types, there must be a lot of constructors and destructors being called.

    Could be, but not necessarily. In a lot of cases, the C++ compiler is able to avoid copying objects (copy elision). Even more so since the introduction of r-value references and move constructors in C++11. And these things are aggressively inlined, even across compilation units. Doing those kinds of optimizations is what makes your code faster, but the compilation slower.

    And std::unique_ptr doesn't have a copy constructor. That's kind of its main purpose (it does have a move constructor however, but that's no more expensive than copying a pointer).