使c ++编译器优化以在映射初始化中调用构造函数一次

时间:2013-06-18 03:03:43

标签: c++ initialization

我有一个生成A类对象的地图的函数。

map<int,A> test()
{
    map<int, A> m;
    A a1(10); // constructor
    A a2(20);
    A a3(30);
    m[0] = a1; m[1] = a2; m[2] = a3; // <-- copy constructor and = operator
    return m;
}

当我执行这个函数时,我调用了构造函数,然后复制构造函数和=运算符调用。

map<int,A> x = test();

有没有办法让编译器优化它来只调用一个像返回值优化(RVO)这样的构造函数呢?

替代方法可能是使用指针,但我想知道是否有另一种方法。

map<int,A*> test3()
{
    map<int, A*> m;
    A* a1 = new A(10);
    A* a2 = new A(20);
    A* a3 = new A(30);
    m[0] = a1; m[1] = a2; m[2] = a3;
    return m;
}

...

map<int,A*> x = test3();

...

for (auto val: x)
{
    delete val.second;
}

2 个答案:

答案 0 :(得分:1)

如果您的实施支持,emplace

#include <map>
#include <iostream>

struct A {
    A(int) { std::cout << "ctor\n"; }
    A(const A&) { std::cout << "copy ctor\n"; }
    A(A&&) { std::cout << "move ctor\n"; }
    ~A() { std::cout << "dtor\n"; }
};

std::map<int,A> test()
{
    std::map<int, A> m;
    m.emplace(0, 10);
    m.emplace(1, 20);
    m.emplace(2, 30);
    return m;
}

int main()
{
    std::map<int, A> m = test();
    std::cout << "m.size() = " << m.size() << '\n';
}

输出:

$ ./test
ctor
ctor
ctor
m.size() = 3
dtor
dtor
dtor

答案 1 :(得分:0)

我也用智能指针测试过,对我来说似乎很好。

#include <iostream>
#include <map>
#include <memory>

using namespace std;

class A
{
    int x;
public: 
    A() {
        cout << "Default constructor called" << endl;
    }  
    A(const A& rhs) 
    {
        this->x = rhs.x;
        cout << "Copy constructor called" << endl;
    }
    A& operator=(const A& rhs)
    {
        this->x = rhs.x;
        cout << "Copy constructor called" << endl;
    }
    A(int x) : x(x) {
        cout << "*I'm in" << endl;
    }
    ~A() {
        cout << "*I'm out" << endl;
    }
    int get() const {return x;}
    void set(int x) {this->x = x;}
};

std::map<int,unique_ptr<A>> test()
{
    std::map<int, unique_ptr<A>> m;
    m[0] = unique_ptr<A>(new A(101));
    m[1] = unique_ptr<A>(new A(201));
    m[2] = unique_ptr<A>(new A(301));
    return m;
}

using namespace std;
int main(int argc, char *argv[]) {

    auto m = test();
    for (const auto& i: m)
    {
        cout << i.second->get() << endl;
    }    
}

结果:

*I'm in
*I'm in
*I'm in
101
201
301
*I'm out
*I'm out
*I'm out