Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > reference type as template argument

Reply
Thread Tools

reference type as template argument

 
 
subramanian100in@yahoo.com, India
Guest
Posts: n/a
 
      05-14-2008
consider
template<typename T> Test
{
// ...
};

We can have a pointer type as argument to a template class. For
example, we can have,
int x = 100;
Test<int*> obj(&x); // assuming a suitable ctor exists

But we cannot have reference type as template argument. This is
because reference cannot be assigned (in the overloaded assignment
operator) in the ordinary sense - that is, a reference cannot be
reseated. So, we cannot instantiate a template class with reference
type as argument. Is this understanding of mine, is correct ?

Kindly clarify.

Thanks
V.Subramanian
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      05-15-2008
On May 14, 5:59 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.com> wrote:
> consider
> template<typename T> Test
> {
> // ...
> };


> We can have a pointer type as argument to a template class. For
> example, we can have,
> int x = 100;
> Test<int*> obj(&x); // assuming a suitable ctor exists


> But we cannot have reference type as template argument.


Sure you can.

> This is because reference cannot be assigned (in the
> overloaded assignment operator) in the ordinary sense - that
> is, a reference cannot be reseated. So, we cannot instantiate
> a template class with reference type as argument. Is this
> understanding of mine, is correct ?


No. Templates don't really pose any requirements on their
arguments, provided it corresponds: the argument to a type
parameter must be a type, the argument to an int parameter an
int, etc.

Actual templates typically will impose contraints, because they
do something with the type they are given. Thus, the containers
in the standard library require the type to be CopyConstructible
and Assignable, because they need to be able to copy construct
and assign objects of that type. You can't use references for
them, because references don't meet the requirements. You can't
use a class type which doesn't support copy construction or
assignment, either.

In practice, just about every real template will impose some
constraints. And it is fairly difficult to write a template
which will work equally well with both references and object
types. (There are some in Boost. But if you look at the code
for them, it's far from trivial.) But note that this is really
no different from functions: it's rather frequent to have
functions which take for example a double or an int, but
restrict the range. Templates are really no different in this
respect.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Class nested inside a template class as template function argument type claudiu C Programming 2 04-01-2011 12:10 PM
r H2 deduce deduce template argument of a template class inheritingfrom a non template base? nguillot C++ 5 03-08-2009 05:56 PM
Template argument as template argument nw C++ 0 04-14-2008 01:36 PM
Design issue : "self type" as a default template argument (recursive template arguments) IR C++ 3 11-22-2006 08:38 PM
How does the compiler interpret a template argument (reference vs. non-reference) ? mrstephengross C++ 2 09-07-2005 03:58 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57