in Education by
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)

1 Answer

0 votes
by
how to return an lvalue in one case and an rvalue in the other case? I suppose you can try with decltype(auto) and a couple of parentheses template decltype(auto) get_number() // "decltype(auto)" instead of "auto" { if constexpr(getref) { return (number); // not "number" but "(number)" } else { return 123123; // just a random number as example } }

Related questions

0 votes
    _________ involves predicting a response with meaningful magnitude, such as quantity sold, stock price, or return on ... R Programming Select the correct answer from above options...
asked Feb 10, 2022 in Education by JackTerrance
0 votes
    I have a function value(x) which is overloaded for many types such that: double value(double x) { return x; } double ... [-Wreturn-local-addr] in the following code: Matrix33 m; m...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    To get the current date, the _______ function will return a Date object which can be converted to a different ... of R Programming Select the correct answer from above options...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    Which function in R language is used to find out whether the means of 2 groups are equal to each other ... Debugging of R Programming Select the correct answer from above options...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    Im using libVLC to playback video in my application, to open local files im using: libvlc_media_new_path(path_to_file ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    How would you write (in C/C++) a macro which tests if an integer type (given as a parameter) is ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Generic iterator (3 answers) Closed 7 years ago. Suppose I want to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Generic iterator (3 answers) Closed 7 years ago. Suppose I want to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Generic iterator (3 answers) Closed 7 years ago. Suppose I want to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 15, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Generic iterator (3 answers) Closed 7 years ago. Suppose I want to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
0 votes
    Consider following piece of code void foo( bool forwad ) { vector::iterator it, end_it; int dir; ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    Consider following piece of code void foo( bool forwad ) { vector::iterator it, end_it; int dir; ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 17, 2022 in Education by JackTerrance
0 votes
    A programmer builds a _________ to avoid repeating the same task or reduce complexity. (a) Function (b) Package ... of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    _________ variables are categorical variables which can hold either string or numeric values. (a) Factor (b) ... of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    R uses _________ scoping^6 0 or static scoping. (a) reflective (b) transitive (c) lexical (d) closure ... Debugging of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
...