Why there is an error?

A

Aries Sun

Hi, guys, recently I read the book "c++ template complete guide",
there is a slice code that I am quite confused. (pasted here). Why
there is an error? Actually I can compile it on Linux, and there is
only an warning.

Thanks!!!

#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

}
 
G

Gianni Mariani

Aries said:
Hi, guys, recently I read the book "c++ template complete guide",
there is a slice code that I am quite confused. (pasted here). Why
there is an error? Actually I can compile it on Linux, and there is
only an warning.

Thanks!!!

#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)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

passes 2 values returns one of those values
{
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)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

passes 3 references returns one of those references.

Maybe the following would work ... ?
char const* const & max (char const* const & a, char const* const & b)
 
A

Aries Sun

yeah, you are right:
char const* const & max (char const* const & a, char const* const &
b)
works, without even a warning.

There is another question about it
template <int bound, typename T>
void zeroOut( T (&ary)[bound] ) {
for( int i = 0; i < bound; ++i )
ary = T();
}
//...
const int hrsinweek = 7*24;
float readings[hrsinweek];
zeroOut( readings ); // bound == 168, T is float

how could this work?
thanks
 
N

Nathen

yeah, you are right:
char const* const & max (char const* const & a, char const* const &
b)
works, without even a warning.

There is another question about it
template <int bound, typename T>
void zeroOut( T (&ary)[bound] ) {
for( int i = 0; i < bound; ++i )
ary = T();}

//...
const int hrsinweek = 7*24;
float readings[hrsinweek];
zeroOut( readings ); // bound == 168, T is float

how could this work?
thanks


I think the T and the bound will resovle in running not compliing.
 
B

Barry

Aries said:
yeah, you are right:
char const* const & max (char const* const & a, char const* const &
b)
works, without even a warning.

There is another question about it
template <int bound, typename T>
void zeroOut( T (&ary)[bound] ) {
for( int i = 0; i < bound; ++i )
ary = T();
}
//...
const int hrsinweek = 7*24;
float readings[hrsinweek];
zeroOut( readings ); // bound == 168, T is float

how could this work?
thanks

Ah, it's quite a long story,
you should read more about "C++ template" and "function template
argument deduction"
 
B

Barry

Nathen said:
I think the T and the bound will resovle in running not compliing.

It's done at compile-time not at run-time.
But hard to explain. I think this is a BIG feature of the C++ language.
 
N

Nathen

----------------------------------------------------------------------------
It's done at compile-time not at run-time.
But hard to explain. I think this is a BIG feature of the C++ language.
--------------------------------------------------------------------------------
oh, I made some mistake.

Would u pls tell me which book could explain this well?
I really want to know the detail,
thx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top