473,398 Members | 2,125 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,398 software developers and data experts.

pass-by-reference for template?

Hello,

I learned that when I work with templates in C++, I should have
functions that pass arguments by reference because the type of object
is not known. Does it mean that if I have a function that is template
function, then it's argument should be a reference type, such as the
following?

template<class T>
void f(T& t){...}

What if I replace the signature by

template<class T>
void f(T t){...}

or

template<class T>
void f(T* tp){...}

Would it work and if not, could someone tell me why please?

Thanks,
Jess

Aug 20 '07 #1
7 5846
"Jess" <wd***@hotmail.comwrote in message
news:11*********************@x35g2000prf.googlegro ups.com...
Hello,

I learned that when I work with templates in C++, I should have
functions that pass arguments by reference because the type of object
is not known. Does it mean that if I have a function that is template
function, then it's argument should be a reference type, such as the
following?

template<class T>
void f(T& t){...}

What if I replace the signature by

template<class T>
void f(T t){...}

or

template<class T>
void f(T* tp){...}

Would it work and if not, could someone tell me why please?
It would work the same way a function would work. So, yes, it would work
the because
void f(int* tp) {/*...*/}
would work.
Aug 20 '07 #2
On 2007-08-20 15:52, Jess wrote:
Hello,

I learned that when I work with templates in C++, I should have
functions that pass arguments by reference because the type of object
is not known. Does it mean that if I have a function that is template
function, then it's argument should be a reference type, such as the
following?
I don't see much connection between templates and using references. In
general, using references is often better than passing by value, whether
you are using templates or not, since it requires less copying.
template<class T>
void f(T& t){...}

What if I replace the signature by

template<class T>
void f(T t){...}
The reference version is slightly better since it does not require the
type used to be copyable. While most types are copyable there are
instances where it might be beneficial to disallow copying.
template<class T>
void f(T* tp){...}
When using pointers you always run the risk of someday getting a NULL-
pointer, with references you don't.

--
Erik Wikström
Aug 20 '07 #3
Thanks for your replies. The reason I asked the question is that I
was told to use reference rather the actual value because T is a
template parameter and it's value isn't known until instantiation. Is
this not so? or, is there some other kind of template functions that
require the use of reference rather than the actual type?

Thanks,
Jess

Aug 22 '07 #4
On Aug 21, 12:40 am, "Alf P. Steinbach" <al...@start.nowrote:
* Erik Wikström:
On 2007-08-20 15:52, Jess wrote:
Hello,
I learned that when I work with templates in C++, I should have
functions that pass arguments by reference because the type of object
is not known. Does it mean that if I have a function that is template
function, then it's argument should be a reference type, such as the
following?
I don't see much connection between templates and using references. In
general, using references is often better than passing by value, whether
you are using templates or not, since it requires less copying.
template<class T>
void f(T& t){...}
What if I replace the signature by
template<class T>
void f(T t){...}
The reference version is slightly better since it does not require the
type used to be copyable. While most types are copyable there are
instances where it might be beneficial to disallow copying.

Well. If T is non-const, then

T&

requires an lvalue actual argument (because an rvalue can't be bound to
a reference).

And if T is const, that is, T is mapped to U const, then with the
current standard

U const&

requires U to have an accessible copy constructor.
Do you mean I can use const T& but not T& here? If I have a non-
template function, I can always have both a const-ref version and a
non-const-ref version. Is this not true for template functions?

Thanks,
Jess

Aug 22 '07 #5
On 2007-08-22 14:59, Jess wrote:
Thanks for your replies. The reason I asked the question is that I
was told to use reference rather the actual value because T is a
template parameter and it's value isn't known until instantiation. Is
this not so? or, is there some other kind of template functions that
require the use of reference rather than the actual type?
In the future please quote the text you are replying to.

It's kind of true but irrelevant, templates are instantiated during
compilation so the compiler knows the type when it needs to. In other
words you won't risk getting your classes sliced or such, like you would
with runtime polymorphism.

The fact that a template is involved have nothing do do with the usage
of references, however using references (or const references) is
generally a good idea (even if you don't work with templates) since it
prevents unnecessary copying.

--
Erik Wikström
Aug 22 '07 #6
On 2007-08-22 15:02, Jess wrote:
On Aug 21, 12:40 am, "Alf P. Steinbach" <al...@start.nowrote:
>* Erik Wikström:
On 2007-08-20 15:52, Jess wrote:
Hello,
>I learned that when I work with templates in C++, I should have
functions that pass arguments by reference because the type of object
is not known. Does it mean that if I have a function that is template
function, then it's argument should be a reference type, such as the
following?
I don't see much connection between templates and using references. In
general, using references is often better than passing by value, whether
you are using templates or not, since it requires less copying.
>template<class T>
void f(T& t){...}
>What if I replace the signature by
>template<class T>
void f(T t){...}
The reference version is slightly better since it does not require the
type used to be copyable. While most types are copyable there are
instances where it might be beneficial to disallow copying.

Well. If T is non-const, then

T&

requires an lvalue actual argument (because an rvalue can't be bound to
a reference).

And if T is const, that is, T is mapped to U const, then with the
current standard

U const&

requires U to have an accessible copy constructor.

Do you mean I can use const T& but not T& here? If I have a non-
template function, I can always have both a const-ref version and a
non-const-ref version. Is this not true for template functions?
You can have both const and non-const with templates, the issue has
nothing to do with templates per se, it's just that const ref is the
most flexible way to pass arguments. Consider the following code:

class Foo
{
Foo(const Foo&);
Foo& operator=(const Foo&);
int v;
public:
Foo(int i) : v(i) {}
};

void val(Foo);

void ref(Foo&);

void cref(const Foo&);

int main()
{
Foo f(1);
// [1]
val(f);
ref(f);
cref(f);

// [2]
ref(Foo(1));
cref(Foo(1));
}

The class Foo can not be copied (the copy ctor is private), the three
function each use different ways to pass parameters. If you try to
compile the code you'll notice that the call to val(f) under [1] does
not work. This is because passing by value requires that you can copy
the object passed. IF you comment out that call you'll see that both
ref(f) and cref(f) works.

Next we'll look at the difference between ref and const ref, to do this
comment out the copy ctor in Foo (this will allow the compiler to
generate one). You'll now notice that ref(Foo(1)) does not compiler but
that cref(Foo(1)) does, this is because a const ref can bind to an
r-value while a normal ref can't.

As you can see a const ref is more versatile than a ref, which is more
versatile than passing by value, plus that you don't get the overhead of
copying the object. So if you can using a const ref is usually a good
idea since it will allow usages that ways wont.

--
Erik Wikström
Aug 22 '07 #7
On Aug 23, 12:00 am, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-08-22 15:02, Jess wrote:
On Aug 21, 12:40 am, "Alf P. Steinbach" <al...@start.nowrote:
* Erik Wikström:
On 2007-08-20 15:52, Jess wrote:
Hello,
I learned that when I work with templates in C++, I should have
functions that pass arguments by reference because the type of object
is not known. Does it mean that if I have a function that is template
function, then it's argument should be a reference type, such as the
following?
I don't see much connection between templates and using references. In
general, using references is often better than passing by value, whether
you are using templates or not, since it requires less copying.
template<class T>
void f(T& t){...}
What if I replace the signature by
template<class T>
void f(T t){...}
The reference version is slightly better since it does not require the
type used to be copyable. While most types are copyable there are
instances where it might be beneficial to disallow copying.
Well. If T is non-const, then
T&
requires an lvalue actual argument (because an rvalue can't be bound to
a reference).
And if T is const, that is, T is mapped to U const, then with the
current standard
U const&
requires U to have an accessible copy constructor.
Do you mean I can use const T& but not T& here? If I have a non-
template function, I can always have both a const-ref version and a
non-const-ref version. Is this not true for template functions?

You can have both const and non-const with templates, the issue has
nothing to do with templates per se, it's just that const ref is the
most flexible way to pass arguments. Consider the following code:

class Foo
{
Foo(const Foo&);
Foo& operator=(const Foo&);
int v;
public:
Foo(int i) : v(i) {}

};

void val(Foo);

void ref(Foo&);

void cref(const Foo&);

int main()
{
Foo f(1);

// [1]
val(f);
ref(f);
cref(f);

// [2]
ref(Foo(1));
cref(Foo(1));

}

The class Foo can not be copied (the copy ctor is private), the three
function each use different ways to pass parameters. If you try to
compile the code you'll notice that the call to val(f) under [1] does
not work. This is because passing by value requires that you can copy
the object passed. IF you comment out that call you'll see that both
ref(f) and cref(f) works.

Next we'll look at the difference between ref and const ref, to do this
comment out the copy ctor in Foo (this will allow the compiler to
generate one). You'll now notice that ref(Foo(1)) does not compiler but
that cref(Foo(1)) does, this is because a const ref can bind to an
r-value while a normal ref can't.

As you can see a const ref is more versatile than a ref, which is more
versatile than passing by value, plus that you don't get the overhead of
copying the object. So if you can using a const ref is usually a good
idea since it will allow usages that ways wont.
Thanks for the examples and explanations! :)
Jess

Aug 23 '07 #8

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

Similar topics

0
by: uli2003wien | last post by:
Dear group, PASS (SQL-Server user group) is about to start founding a branch in Vienna. I went to SQLCON and met some guys from the German PASS group and decided to become a member of PASS,...
8
by: Tcs | last post by:
I've been stumped on this for quite a while. I don't know if it's so simple that I just can't see it, or it's really possible. (Obviously, I HOPE it IS possible.) I'm trying to get my queries...
2
by: Alex Nitulescu | last post by:
Hi. I have tried to pass two parameters, like this: Response.Redirect(String.Format("NewPage.aspx?Username={0}, Pass={1}", txtUserName.Text, txtPass.Text)) But if I pass Username="Alex" and...
3
by: Brett | last post by:
I have several classes that create arrays of data and have certain properties. Call them A thru D classes, which means there are four. I can call certain methods in each class and get back an...
4
by: Marcelo | last post by:
Any suggestion? Thanks Marcelo
3
by: ILCSP | last post by:
Hello, I'm fairly new to the concept of running action pass through queries (insert, update, etc.) from Access 2000. I have a SQL Server 2000 database and I'm using a Access 2K database as my...
3
by: Ronald S. Cook | last post by:
I want to something as simple as: UserControl uctTemp; But the type will be passed in to the function (which will be an existing user control like "uctMyUserControl1") So, how can I pass in...
5
by: marshmallowww | last post by:
I have an Access 2000 mde application which uses ADO and pass through queries to communicate with SQL Server 7, 2000 or 2005. Some of my customers, especially those with SQL Server 2005, have had...
5
by: Remote_User | last post by:
Hi, Is there any way I can pass a file as a parameter? ( maybe read it into a object) I am working on C# VS2005. Thanks.
14
by: =?Utf-8?B?Umljaw==?= | last post by:
I have seen examples of passing data from FormB to FormA (Parent To Child) but I need an example of passind data from FormA to FormB. My main form is FormA; I will enter some data in textboxes and...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.