providing a default argument to a function, but that argument is a struct

With a compiler supporting C++17 this becomes a simple matter of using std::optional with std::reference_wrapper. Implicit conversions do all the hard work for you.

#include <optional>
#include <utility>
#include <functional>

struct mat4x4
{
    float m[4][4] = {0};
};

mat4x4 matrix_matrix(mat4x4 m1, mat4x4 m2,
             std::optional<std::reference_wrapper<mat4x4>
             > m3=std::nullopt)
{
    mat4x4 new_m;

    // Calculate something in new_m;

    if (m3)
        *m3=new_m;

    return new_m;
}

void test()
{
    mat4x4 m1, m2, m3, mret;

    mret=matrix_matrix(m1, m2);

    mret=matrix_matrix(m1, m2, m3);
}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top