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 7495843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:21:06+00:00 2026-05-29T18:21:06+00:00

This example is from C++ templates book by Josuttis : #include <iostream> #include <cstring>

  • 0

This example is from C++ templates book by Josuttis :

 #include <iostream>
 #include <cstring>
 #include <string>

 // maximum of two values of any type (call-by-reference)
 template <typename T>
 inline T const& max (T const& a, T const& b)
 {
     return  a < b  ?  b : a;
 }

 // maximum of two C-strings (call-by-value)
 inline char const* max (char const* a, char const* b)
 { 
    return  std::strcmp(a,b) < 0  ?  b : a;
 }

 // maximum of three values of any type (call-by-reference)
 template <typename T>
 inline T const& max (T const& a, T const& b, T const& c)
 {
     return max (max(a,b), c);  // error, if max(a,b) uses call-by-value
 }

 int main ()
 {
   ::max(7, 42, 68);     // OK

    const char* s1 = "frederic";
    const char* s2 = "anica";
    const char* s3 = "lucas";
    ::max(s1, s2, s3);    // ERROR

}

He says that the reason for error in ::max(s1, s2, s3) is that for C-strings max(max(a,b),c) calls max(a,b) that creates a new temporary local value that may be returned by the function by reference.

I am not getting how a new local value is getting created ?

  • 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-05-29T18:21:09+00:00Added an answer on May 29, 2026 at 6:21 pm

    For C-strings, this code creates a local value, i.e, a local variable that stores an address (pointer of type char const *):

    std::strcmp(a,b) < 0 ? b : a;

    Hence returning a reference to this (using the template functions) leads to an error. In this case the reference of type char const * const & to a local is returned by the template function max after the C-string max has returned a copy. The template functions have to return pointers by value instead of reference.

    The template functions need to be overloaded for pointer types.

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

Sidebar

Related Questions

thank you for looking i got this example from my book but i cant
I wrote a example from a book that uses two classes. A IntStack and
Hey guys, i'm trying this example from Dave Ward blog about jQuery Templates and
In this example from The Ruby Programming Language (p.270), I'm confused why the instance_eval
In this example from the App Engine docs , why does the example declare
I tried to copy this example from this Multiprocessing lecture by jesse noller (as
With regards this example from Code Complete: Comparison Compare(int value1, int value2) { if
Why in this example from MSDN, in GetEnumerator method, PeopleEnum returns IEnumerator ? public
I am trying to follow this example (from p137 of Rob Pickering's Foundations of
Can someone explain how New works with the With keyword in this example from

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.