473,387 Members | 1,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Template notation

I am trying to understand the following template:

template <typename T>
inline T const& max (T const& a, T const& b) {
return a < b ? b : a;
}

does it say that the function max returns a constant reference to T of
any type?

I have never seen a function returning references before, does it mean
that it returns the address of the value returned?

normally the address of eg. an integer is:

int x = 22;

&x

but in the above function its more like:

x&
Apr 23 '07 #1
6 1894
On 23 Apr, 14:30, desktop <f...@sss.comwrote:
I am trying to understand the following template:

template <typename T>
inline T const& max (T const& a, T const& b) {
return a < b ? b : a;

}

does it say that the function max returns a constant reference to T of
any type?
It returns a const reference of type T, yes.
I have never seen a function returning references before, does it mean
that it returns the address of the value returned?
No, pointers are not references, a reference can be seen as an alias
for a variable, so the following is true for references but not for
pointers:

int i = 1;
int& j = i; // j is a reference to i
if (&i == &j)
// Always true

So you can see that if you take the address of a reference to an
object you get the address of the object while if you take the address
of a pointer to a object you get that pointer's address.

Another example:

void foo(int& i) {
++i;
}

int k = 1;

foo(k);

Now, in the function foo(), the i we are changing is the same integer
as k, so it's another name but the same variable.

--
Erik Wikström

Apr 23 '07 #2
Erik Wikström wrote:
On 23 Apr, 14:30, desktop <f...@sss.comwrote:
>I am trying to understand the following template:

template <typename T>
inline T const& max (T const& a, T const& b) {
return a < b ? b : a;

}

does it say that the function max returns a constant reference to T of
any type?

It returns a const reference of type T, yes.
>I have never seen a function returning references before, does it mean
that it returns the address of the value returned?

No, pointers are not references, a reference can be seen as an alias
for a variable, so the following is true for references but not for
pointers:

int i = 1;
int& j = i; // j is a reference to i
if (&i == &j)
// Always true

So you can see that if you take the address of a reference to an
object you get the address of the object while if you take the address
of a pointer to a object you get that pointer's address.

Another example:

void foo(int& i) {
++i;
}

int k = 1;

foo(k);

Now, in the function foo(), the i we are changing is the same integer
as k, so it's another name but the same variable.

--
Erik Wikström

Ok guess it the same as in java where Object myobj = new Object()
generates the reference myobj to an Object.
The parameters to the max template:

max (T const& a, T const& b)

are also references, why pass a reference to a template instead of the
"real" object?
Apr 23 '07 #3
desktop wrote:
[..]
Ok guess it the same as in java where Object myobj = new Object()
generates the reference myobj to an Object.
I don't think it's appropriate to say that. There are no references
in Java (Just like there are no pointers in Java).
The parameters to the max template:

max (T const& a, T const& b)

are also references, why pass a reference to a template instead of the
"real" object?
Please see FAQ section 8, section 31, the rest of it too. If you have
questions left after that, ask.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 23 '07 #4
On Apr 23, 9:13 am, desktop <f...@sss.comwrote:
Erik Wikström wrote:
On 23 Apr, 14:30, desktop <f...@sss.comwrote:
I am trying to understand the following template:
template <typename T>
inline T const& max (T const& a, T const& b) {
return a < b ? b : a;
}
does it say that the function max returns a constant reference to T of
any type?
It returns a const reference of type T, yes.
I have never seen a function returning references before, does it mean
that it returns the address of the value returned?
No, pointers are not references, a reference can be seen as an alias
for a variable, so the following is true for references but not for
pointers:
int i = 1;
int& j = i; // j is a reference to i
if (&i == &j)
// Always true
So you can see that if you take the address of a reference to an
object you get the address of the object while if you take the address
of a pointer to a object you get that pointer's address.
Another example:
void foo(int& i) {
++i;
}
int k = 1;
foo(k);
Now, in the function foo(), the i we are changing is the same integer
as k, so it's another name but the same variable.
--
Erik Wikström

Ok guess it the same as in java where Object myobj = new Object()
generates the reference myobj to an Object.
Java might call that a reference, its closer to a C++ pointer.
Java has no equivalence of a C++ reference.
>
The parameters to the max template:

max (T const& a, T const& b)

are also references, why pass a reference to a template instead of the
"real" object?
Its the other way around. passing by reference IS passing the "real"
variable using an alias.
Passing by value only modifies a local variable and more importantly -
it invokes a constructor, usually a copy ctor.

Apr 23 '07 #5
Salt_Peter wrote:
Passing by value only modifies a local variable and more importantly -
it invokes a constructor, usually a copy ctor.
Quite curiously, passing by reference may in some cases actually
produce less efficient code (depending on the compiler, of course).

Using gcc 3.3.5 with maximum optimizations (-O3 -march=pentium4)
this code:

template<typename T>
inline const T& max(T const& a, T const& b) { return a < b ? b : a; }
int foo(int a, int b) { return max(a, b); }

produces this asm:

..LFB5:
pushl %ebp
..LCFI0:
movl %esp, %ebp
..LCFI1:
movl 12(%ebp), %edx
leal 8(%ebp), %eax
leal 12(%ebp), %ecx
cmpl %edx, 8(%ebp)
popl %ebp
cmovge %eax, %ecx
movl (%ecx), %eax
ret

Removing the references should *in theory* produce an identical
result, but it doesn't. The code:

template<typename T>
inline const T max(T const a, T const b) { return a < b ? b : a; }
int foo(int a, int b) { return max(a, b); }

produces this asm:

..LFB5:
pushl %ebp
..LCFI0:
movl %esp, %ebp
..LCFI1:
movl 8(%ebp), %ecx
movl 12(%ebp), %eax
popl %ebp
cmpl %eax, %ecx
cmovge %ecx, %eax
ret

The 10 opcodes have been reduced to 8. Granted, I'm not a Pentium4
expert and I can't tell for sure that the latter code is faster than
the former, but I'm pretty convinced.
Apr 24 '07 #6
On Apr 23, 11:45 pm, Juha Nieminen <nos...@thanks.invalidwrote:
Salt_Peter wrote:
Passing by value only modifies a local variable and more importantly -
it invokes a constructor, usually a copy ctor.

Quite curiously, passing by reference may in some cases actually
produce less efficient code (depending on the compiler, of course).

Using gcc 3.3.5 with maximum optimizations (-O3 -march=pentium4)
this code:

template<typename T>
inline const T& max(T const& a, T const& b) { return a < b ? b : a; }
int foo(int a, int b) { return max(a, b); }

produces this asm:

.LFB5:
pushl %ebp
.LCFI0:
movl %esp, %ebp
.LCFI1:
movl 12(%ebp), %edx
leal 8(%ebp), %eax
leal 12(%ebp), %ecx
cmpl %edx, 8(%ebp)
popl %ebp
cmovge %eax, %ecx
movl (%ecx), %eax
ret

Removing the references should *in theory* produce an identical
result, but it doesn't. The code:

template<typename T>
inline const T max(T const a, T const b) { return a < b ? b : a; }
int foo(int a, int b) { return max(a, b); }

produces this asm:

.LFB5:
pushl %ebp
.LCFI0:
movl %esp, %ebp
.LCFI1:
movl 8(%ebp), %ecx
movl 12(%ebp), %eax
popl %ebp
cmpl %eax, %ecx
cmovge %ecx, %eax
ret

The 10 opcodes have been reduced to 8. Granted, I'm not a Pentium4
expert and I can't tell for sure that the latter code is faster than
the former, but I'm pretty convinced.
With simple variables thats expected and in this case pass by value is
fine if T remains a primitive type. The moment you start passing
complex variables by value (and invoking additional copy constructors)
the results won't be the same.
Apr 24 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Gianni Mariani | last post by:
Does anyone know if this is supposed to work ? template <unsigned N> int strn( const char str ) { return N; } #include <iostream>
6
by: Ernesto | last post by:
Hola gurús: I'm programming my own collection classes and I already implemented a DynamicArray defined as: template <class T> class DynamicArray { ... };
3
by: CoolPint | last post by:
After upgrading to gcc 3.4.2 from gcc 3.2.3, I got compiler errors that I could not figure out. After reading other postings, I learned that my coding was not compliant to the standard in the first...
3
by: danilo.horta | last post by:
Hi folks I'm having a scope resolution issue. The gnu compiler is trying to use the "operator function" from derived class rather than from correct one, the base class. // VecBasis.h file...
1
by: aidy | last post by:
Hi, I have created this XML for a test log <SNIP> <?xml version="1.0"?> <?xml-stylesheet type='text/xsl' href='show.xsl'?> <testresults> <test id="TESTID_10"> <teststatus>pass</teststatus>
2
by: cgv | last post by:
Hi, I want to distinguish between an int parameter to a template function depending on whether its value is known at compile time or not. For the compile time version I want to call the function...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
5
by: desktop | last post by:
I am confused about the use of the template parameter "E" in the below class. Since when is it allowed to use these parameters like "E(1)" and what does it mean (where can I read more about this...
8
by: Bryan | last post by:
I have a template class declared like this: template <class T> class CKineStore { public: CKineStore(void) {}; virtual ~CKineStore(void) {}; public:
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.