C++11 pattern for factory function returning tuple

c++, c++11, factory

Solution

How do I get around that without having to std::get<> each item? Is there an elegant way to do this?

Return by value, instead of returning by "values" (which is what this std::tuple allows you to do).

API changes:

class Wavefront
{
public:
    Wavefront(VAO v, Mesh m, ShaderProgram sp); // use whatever construction
                                                // suits you here; you will
                                                // only use it internally
                                                // in the load function, anyway
    const VAO& vao() const;
    const Mesh& mesh() const;
    const ShaderProgram& shader() const;
};

Wavefront LoadWavefront(std::string filename);

Problem

In my project I have some functions like ``` std::tuple<VAO, Mesh, ShaderProgram> LoadWavefront(std::string filename); ``` That I can use like this: ``` VAO teapotVAO; Mesh teapotMesh; ShaderProgram teapotShader; std::tie(teapotVAO, teapotMesh, teapotShader) = LoadWavefront("assets/teapot.obj"); ``` The problem is, this requires each of those classes to have a default constructor that creates them in an invalid state, which is error prone. How do I get around that without having to `std::get<>` each item? Is there an elegant way to do this?

Original source

Related problems