copy elision visible side effect

后端 未结 1 1701
耶瑟儿~
耶瑟儿~ 2021-01-24 10:02

Consider this code:

#include 

using namespace std;

struct Foo {
public:
    int _a{};
    Foo(int a) : _a{a} 
    {
        std::cout <<          


        
相关标签:
1条回答
  • 2021-01-24 10:26

    The whole point of singling copy elision out as a on optimization in specific cases is to allow eliding side effects of copy construction. That is, yes, it expected that copy elision happens despite the copy constructor and/or the destructor having side effects.

    If you don't want copy elision to happen in certain cases you'll need to arrange for it to be inhibited. In your specific case it is admittedly a bit annoying to inhibit copy elision but something like this should do the trick:

    template <typename T>
    T const& inhibit(T const& ref) {
        return ref;
    }
    // ...
    Foo b = inhibit<Foo>(10);
    
    0 讨论(0)
提交回复
热议问题