Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can we declare lvalue inplace while calling a function with lvalue reference parameter?

If I have a function like this:

int calc(const DataVec& data_vec, int& sub_sum);

how can I call that without a explicit lvalue definition of type int?

auto calc_result = calc(data_vec, int()); // error, int() is not a lvalue

Below is a valid form:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

int _;  // not interested
auto calc_result = calc(data_vec, _);

>Solution :

This can be solved with function overloading. Like

int calc(const DataVec& data_vec);

Your overloaded function could be a simple wrapper around your dummy-int variable workaround:

int calc(const DataVec& data_vec)
{
    int dummy = 0;
    return calc(data_vec, dummy);
}

Please note that this might be a suitable workaround to the problem. But it might as well be a workaround for a problem that doesn’t really exist. Perhaps there is a very good reason for the original calc functions second argument being a non-const lvalue reference?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading