相关疑难解决方法(0)

g ++:如果涉及多个翻译单元,RVO如何工作

首先,请查看以下代码,其中包含2个翻译单元.

--- foo.h ---

class Foo
{
public:
    Foo();
    Foo(const Foo& rhs);
    void print() const;
private:
    std::string str_;
};

Foo getFoo();

--- foo.cpp ---
#include <iostream>

Foo::Foo() : str_("hello")
{
    std::cout << "Default Ctor" << std::endl;
}

Foo::Foo(const Foo& rhs) : str_(rhs.str_)
{
    std::cout << "Copy Ctor" << std::endl;
}

void Foo:print() const
{
    std::cout << "print [" << str_ << "]" << std:endl;
}

Foo getFoo()
{
    return Foo(); // Expecting RVO
}

--- main.cpp ---
#include "foo.h"

int …
Run Code Online (Sandbox Code Playgroud)

c++ g++ rvo

10
推荐指数
2
解决办法
686
查看次数

标签 统计

c++ ×1

g++ ×1

rvo ×1