If you have a function that if constexpr () decides to do one thing or the other, how to return an lvalue in one case and an rvalue in the other case?
The following example does not compile in the first usage line, because the return type auto is no reference:
static int number = 15;
template
auto get_number(int sometemporary)
{
if constexpr(getref)
{
return number; // we want to return a reference here
}
else
{
(...) // do some calculations with `sometemporary`
return sometemporary;
}
}
void use()
{
int& ref = get_number(1234);
int noref = get_number(1234);
}
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)