473,385 Members | 1,888 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,385 software developers and data experts.

reference type as template argument

consider
template<typename TTest
{
// ...
};

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
Jun 27 '08 #1
1 5380
On May 14, 5:59 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
consider
template<typename TTest
{
// ...
};
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:ja*********@gmail.com
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
Jun 27 '08 #2

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

Similar topics

2
by: John Harrison | last post by:
I want to write a templated constructor with a non-type template argument, like this. class X { public: X() : val(0) {} template <int I> X(X const&, X const&) : val(I) {}
8
by: vpadial | last post by:
Hello, I want to build a library to help exporting c++ functions to a scripting languagge. The scripting language provides a function to register functions like: ANY f0() ANY f1(ANY) ANY...
14
by: Bart Samwel | last post by:
Hi everybody, I would really like some help explaining this apparent discrepancy, because I really don't get it. Here is the snippet: void foo(int&); void foo(int const&); ...
2
by: Default User | last post by:
I received some code from another programmer who happens to be out of town. In this, he had something like: struct st { std::string s<20>; }; Looks like it's setting the string size.
2
by: mrstephengross | last post by:
I'd like to know if there's a standard way that the compiler interprets a template argument. That is, let's say you pass a Foo instance as a template argument to some function. Is the instance...
3
by: IR | last post by:
Hi, I've been trying to do the following (which doesn't compile) : template<class T, class F = Example<T struct Example { F foo(); };
7
by: Jess | last post by:
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...
1
by: subramanian100in | last post by:
consider template<T iclass Test { //... }; Here 'T' should only be int. ie we can only have template<int iclass Test {
9
by: Marco Nef | last post by:
Hi there I'm looking for a template class that converts the template argument to a string, so something like the following should work: Convert<float>::Get() == "float"; Convert<3>::Get() ==...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
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?
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
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,...

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.