Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8566623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:41:48+00:00 2026-06-11T17:41:48+00:00

Is the below function definition is legal or not? T& GetMax(const T& t1, const

  • 0

Is the below function definition is legal or not?

T& GetMax(const T& t1, const T& t2)
{
    if (t1 > t2)
    {
        return t2;
    }
    // else 
    return t2;
}

It is written that :
“At the return statements, compiler would complain that t1 or t2 cannot be converted to non-const.”
I read it in this site : http://www.codeproject.com/Articles/257589/An-Idiots-Guide-to-Cplusplus-Templates-Part-1

Does it mean that it is illegal ,if not what else? Could you provide example of use of it?
Could you provide some clear explanation to me ?
Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:41:49+00:00Added an answer on June 11, 2026 at 5:41 pm

    Why should it be illegal? Your function can use some global state to return, while it is forbidden to return its arguments.

    int& foo(const int& a, const int& b)
    {
        static int c = a + b;
        return c;
    }
    

    ADDED

    You cannot return one of your argument, because it will violate const constrains. You can

    • return from function by value
    • use non-const argument types

    if returning non-const in your case would be possible, one could write

    foo(5, 4) = 3;
    

    which has no sense.

    You can not also return reference to some temporary automatic variable, created inside function, as it will be destroyed, when function finished.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like the below function to be more flexible and accept multiple callbacks
AIM: I would like to set the below function to call every 5 seconds.
There is a script error in the function definition below function foo () {
Armed with underscore , I could achieve something fantastic like below: _([1,2,3]).chain().map(function(n) {return n*2}).reduce(function(m,a)
When i invoke a virtual function from a base constructor, the compiler does not
Below is a function from JQueryTools and I have never seen the following definition
When I put the below function into leon repl , I get java.lang.IllegalArgumentException: Wrong
I am using the below function of javascript to enable and disable the radio
Anyone experienced exception using below function? tdse.GetObject(tmpFolderWebDavURL, EnumOpenMode.OpenModeView, null, XMLReadFilter.XMLReadAll) as Folder; Seems if
let say i have function like below function doSomethingNow(){ callSomethingInFutureNotExistNow(); } at the moment

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.