473,799 Members | 3,006 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reference type as template argument

consider
template<typena me 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 5481
On May 14, 5:59 pm, "subramanian10. ..@yahoo.com, India"
<subramanian10. ..@yahoo.comwro te:
consider
template<typena me 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 CopyConstructib le
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 objektorientier ter Datenverarbeitu ng
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
1249
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
11368
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 f2(ANY, ANY) ANY f3(ANY, ANY, ANY)
14
2907
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&); template<typename T>
2
1648
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
1413
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 itself passed, or does the compiler assume it's a Foo reference? For example: ================================ template<typename I> someFunc(I i) { /* ... */ } Foo f;
3
2489
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
5905
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 that is template function, then it's argument should be a reference type, such as the following? template<class T> void f(T& t){...}
1
1186
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
17581
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() == "3"; Convert<HelloWorld>::Get() == "HelloWorld"; The reason I need this is that in our design every class of a certain
0
9546
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10491
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10247
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9079
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7571
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.