简体   繁体   中英

Is it possible to avoid copy-constructor call in C++

I am writing a template function which accepts a custom class (that can be any class or primitive type) as a template argument, then reads some data (of that type) from an input stream, and then stores it an unordered map simmilar to this one:

std::unordered_map<CustomClass, std::vector<CustomClass>> map;

I have implemented a custom class to test the behavior. I have overloaded the std::hash so that ithis class can be stored in an unordered map as a key and overloaded all operators and cosntructors such that whenever they are called, i get a message in the console (example, when a copy cosntstructor is called, i get a message "copy construcot [..data...]")

EDIT: As requested in the comments, here is the custom class definition and implementation (please note: the class here is only a placeholder so we can discuss the general idea behind this question. I am well aware that it is dumb and should not be implemented like this. The code for operators >> and << is not here, to avoid clutter)

class CustomClass{
public:
    CustomClass(int a=0){
        std::cout << "default constructor" << std::endl;
        m_data = a;
    }

    CustomClass(const CustomClass& other){
        std::cout << "copy constructor "  ;//<< std::endl;
        m_data = other.m_data;
        std::cout << "[" << m_data << "]"  << std::endl;
    }

    CustomClass(CustomClass&& other){
        std::cout << "move cosntructor" << std::endl;
        m_data = other.m_data;
    }

    CustomClass& operator=(const CustomClass& other){
        std::cout << "copy assignment operator" << std::endl;
        if(this != &other){
           m_data = other.m_data;
        }
        return *this;
    }

    CustomClass& operator=(CustomClass&& other){
        std::cout << "move assignment operator" << std::endl;
        if(this != &other){
            m_data = other.m_data;
        }
        return *this;
    }

    ~CustomClass(){
        std::cout << "destructor" << std::endl;
    }

    int m_data;

};

Now my question is this: Is it possible to read data from the input stream and construct it inplace where it is needed without a copy constructor call? Example of some code:

CustomClass x1;                        // deafult constructor call
CustomClass x2;                        // deafult constructor call
std::cout << "----" << std::endl;
std::cin >> x1 >> x2;                  // my input
std::cout << "----" << std::endl;
map[x1].emplace_back(x2);              // 2 copy constructor calls
std::cout << "----" << std::endl;
std::cout << map[x1][0] << std::endl;  //operator==  call
std::cout << "----" << std::endl;

And here is an exmaple output from that code:

default constructor
default constructor
----
[1]
[2]
----
copy constructor [1] 
copy constructor [2]
----
operator ==
[2]
----
destructor
destructor
destructor
destructor

I would like to have it so that every object of this class is constructed only once. Is it possible to avoid these copy constructors? If not both, then at least the one that is called during the emplace_back() call? Is it possible to construct the object in the vector exactly where it needs to be in memory but that this sort of call works for every type?

If i need to further elaborate on my question please tell me in the comments, I will be happy to do so

So you have an std::vector and you want to put an element there avoiding unnecessary copying. Assuming that move constructor for your class is cheap, the first option is to define a non-trivial constructor, read the parameters from stream and then emplace_back a newly created object:

using CustomClass = std::vector<int>;

std::vector<CustomClass> v;
size_t size;
int value;
std::cin >> size >> value;
v.emplace_back(size, value);

Here I defined the CustomClass as a vector of integers, and it has a constructor that takes 2 parameters: size and the value. For sure it is cheaper to read there two integers and create the instance of CustomClass only once and using the emplace_back for this purpose, rather than to create the instance and copy it using push_back :

using CustomClass = std::vector<int>;

std::vector<CustomClass> v;
size_t size;
int value;
std::cin >> size >> value;
CustomClass instance(size, value);
v.push_back(instance);

This however doesn't give you much benefits to compare with pushing back the r-value:

using CustomClass = std::vector<int>;

std::vector<CustomClass> v;
size_t size;
int value;
std::cin >> size >> value;
v.push_back(CustomClass(size, value));

Anyway, you need to keep in mind that both push_back and emplace_back may require reallocation of the elements, and that may be inefficient, especially if your CustomClass has no no-throwing move constructor.

Another problem could be if your class doesn't have a reasonable constructor (or the size of the values you need to pass to the constructor are almost the size of the object). In this case I'm offering you the solution to resize() and read to the back()

If the reallocation is something that you are not afraid of (for example you know the number of elements ahead of time and reserve the buffer), you can do the following:

std::vector<CustomClass> v;

v.resize(v.size() + 1);
std::cin >> v.back();

In this case you create a default value once and then read the contents.

Another solution could be to pass the std::istream to the constructor of CustomClass :

class CustomClass {
public:
    CustomClass(std::istream&);
};

std::vector<CustomClass> v;
v.emplace_back(cin);

Update:

Assuming that you know nothing about the actual type of the CustomClass, the most generic (not fully generic, as it still requires default constructor to be able to be pushed with resize() ) is to use resize() / back() idiom.

This is how you do it (avoids any unnecessary ctor calls, including a default one):

#include <vector>
#include <unordered_map>
#include <cstdio>
#include <iostream>


using namespace std;


//--------------------------------------------------------------------------
template <class F> struct inplacer
{
    F f;
    operator invoke_result_t<F&>() { return f(); }
};

template <class F> inplacer(F) -> inplacer<F>;


//--------------------------------------------------------------------------
struct S
{
    S(istream&) { printf("istream ctor\n" ); }
    S()         { printf("ctor\n" ); }
    ~S()        { printf("dtor\n" ); }
    S(S const&) { printf("cctor\n"); }
    S(S&&)      { printf("mctor\n"); }
    S& operator=(S const&) { printf("cop=\n"); return *this; }
    S& operator=(S&&)      { printf("mop=\n"); return *this; }
    friend bool operator==(S const& l, S const& r) { return &l == &r; } //!! naturally, this needs proper implementation
};

template<> struct std::hash<S>
{
    size_t operator()(S const&) const noexcept { return 0; } //!! naturally, this needs proper implementation
};


//--------------------------------------------------------------------------
template<class R> struct read_impl;   // "enables" partial specialization

template<class R> R read(istream& is)
{
    return read_impl<R>::call(is);
}

template<> struct read_impl<S>
{
    static auto call(istream& is) { return S(is); }
};

template<class T> struct read_impl<vector<T>>
{
    static auto call(istream& is)
    {
        vector<T> r; r.reserve(2);          //!! naturally you'd read smth like length from 'is'
        for(int i = 0; i < 2; ++i)
            r.emplace_back(inplacer{[&]{ return read<T>(is); }});
        return r;
    }
};

template<class K, class V> struct read_impl<unordered_map<K, V>>
{
    static auto call(istream& is)
    {
        unordered_map<K, V> r;
        r.emplace( inplacer{[&]{ return read<K>(is); }}, inplacer{[&]{ return read<V>(is); }} );
        return r;
    }
};


//--------------------------------------------------------------------------
auto m = read<unordered_map<S, vector<S>>>(cin);

As you can see in the output -- you end up with 3 "istream ctor" calls and 3 "dtor" calls.

As for iostreams -- stay away from them if you care about performance, clarity, etc... The most ridiculous library ever.

PS "Partial specialization for function templates" trick is stolen from here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM