samedi 27 mai 2017

How to return object more efficient without copying

I want to implement a small C++ helper that reads a given file into a std::string.

This is what I have:

    std::string ReadFile(const std::string& filePath)
    {
        std::ifstream fileStream(filePath);
        std::string fileContent((std::istreambuf_iterator<char>(fileStream)), std::istreambuf_iterator<char>());
        return fileContent;
    }

This piece of code generates a string and then returns a copy of it. I could void the function and take a std::string& as second parameter to read the data into. But I wonder if there is a more efficient way to do this?!

Aucun commentaire:

Enregistrer un commentaire