473,545 Members | 2,032 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function templates

al
1. If a function template is defined:

template <typename A>
A SQ(A& a)
{
return a*a;
}

How could prevent users from passing a type which makes no sense, like
Employee class type?

2. If Employee is pass as T in the template below, should an assignment
operator have to be defined since there're assignment operation in the
function?

template <class T>
void swap(T& x, T& y)
{
T temp;
temp = x;
x = y;
y = temp;
}

3. Is "template <class T>" equivalent to "template <typename T>"?

Thanks!

Jul 22 '05 #1
13 1656

"al" <al***@168.ne t> wrote in message news:ne******** *************** @bgtnsc04-news.ops.worldn et.att.net...
1. If a function template is defined:

How could prevent users from passing a type which makes no sense, like
Employee class type?
You can't, anymore than you can stop them from writing
Employee() * Employee()
other than the compiler bitching about * not being defined for those
types.

2. If Employee is pass as T in the template below, should an assignment
operator have to be defined since there're assignment operation in the
function?
There has to be one accessible. Of course, operator= will be defined
in a class if the user doesn't declare one.
3. Is "template <class T>" equivalent to "template <typename T>"?


Yes. <class T> will match any type, not just class types. Typename
and class are interchangeable in this context.

Jul 22 '05 #2
"al" <al***@168.ne t> wrote in message
news:ne******** *************** @bgtnsc04-news.ops.worldn et.att.net...
1. If a function template is defined:

template <typename A>
A SQ(A& a)
{
return a*a;
}

How could prevent users from passing a type which makes no sense, like
Employee class type?
Let 'em try. It won't compile. Of course the error message may be a bit
obscure.

2. If Employee is pass as T in the template below, should an assignment
operator have to be defined since there're assignment operation in the
function?

template <class T>
void swap(T& x, T& y)
{
T temp;
temp = x;
x = y;
y = temp;
}
If you don't write an assignment operator the compiler will provide you with
one which just copies the bits.

3. Is "template <class T>" equivalent to "template <typename T>"?
Yes. The keyword "typename" was a relatively late addition to the language.
It makes more sense in this context then "class".

Thanks!


--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #3
al wrote:
1. If a function template is defined:

template <typename A>
A SQ(A& a)
{
return a*a;
}

How could prevent users from passing a type which makes no sense, like
Employee class type?


You can create a specialization like:

template <typename A>
A SQ(A& a)
{
return a*a;
}

template <>
Employee SQ(Employee& a)
{
... some code that compiles to an error ...
}

However if you don't provide a 'operator *()', your original SQ provides
for an error anyway.
Jul 22 '05 #4

"Cy Edmunds" <ce******@spaml ess.rochester.r r.com> wrote in message news:Wx******** ***********@twi ster.nyroc.rr.c om...
If you don't write an assignment operator the compiler will provide you with
one which just copies the bits.


Well, more specifically, it invokes the assignment semantics of all the subobjects
(bases and non-static members).


Jul 22 '05 #5
"Ron Natalie" <ro*@sensor.com > wrote in message
news:3f******** *************** @news.newshosti ng.com...

"Cy Edmunds" <ce******@spaml ess.rochester.r r.com> wrote in message news:Wx******** ***********@twi ster.nyroc.rr.c om...
If you don't write an assignment operator the compiler will provide you with
one which just copies the bits.
Well, more specifically, it invokes the assignment semantics of all the

subobjects (bases and non-static members).


Right! When I first started C++ I worried about that.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #6
Chris Mantoulidis wrote:
"Cy Edmunds" <ce******@spaml ess.rochester.r r.com> wrote in message
news:<Wx******* ************@tw ister.nyroc.rr. com>...
"al" <al***@168.ne t> wrote in message
news:ne******** *************** @bgtnsc04-news.ops.worldn et.att.net...
> 1. If a function template is defined:
>
> template <typename A>
> A SQ(A& a)
> {
> return a*a;
> }
>
> How could prevent users from passing a type which makes no sense,
> like Employee class type?
Let 'em try. It won't compile. Of course the error message may be a
bit obscure.
>
> 2. If Employee is pass as T in the template below, should an
> assignment operator have to be defined since there're assignment
> operation in the function?
>
> template <class T>
> void swap(T& x, T& y)
> {
> T temp;
> temp = x;
> x = y;
> y = temp;
> }


If you don't write an assignment operator the compiler will provide
you with one which just copies the bits.
>
> 3. Is "template <class T>" equivalent to "template <typename T>"?


Yes. The keyword "typename" was a relatively late addition to the
language. It makes more sense in this context then "class".

So, <class T> allows ANY KINDA class and type (int, char, etc.) to be
passed as the template whereas <template T> allows just types??


What do you mean by "just types"? classes _are_ types.
Is that it? Cuz I'm a bit confused.


Again, there is no difference between <class T> and <typename T> (not
<template T> btw - I guess that was a typo).

Jul 22 '05 #7
Jacques Labuschagne <ja*****@clawsh rimp.com> wrote in message news:<vo******* *************@n ews02.tsnz.net> ...
Chris Mantoulidis wrote:
So, <class T> allows ANY KINDA class and type (int, char, etc.) to be
passed as the template whereas <template T> allows just types?? Is
that it? Cuz I'm a bit confused.
No, the two are equivalent. You can't restrict T, but you can use T in
such a way that it only compiles with compatible types.


compatible types = int, char, etc. ???
Then what's the difference between typename and class... You don't really explain

Thanks,
cmad

template<typena me T>
void foo(const T& some_object){
some_object.unu sual_function() ;
}

or even

template<typena me T>
T bar(const T& some_object){
T other_object; // requires T default constructable.
other_object = some_object; // requires T copyable.
return some_object + other_object; // requires operator+
}

Jul 22 '05 #8
Chris Mantoulidis wrote:
Jacques Labuschagne <ja*****@clawsh rimp.com> wrote in message news:<vo******* *************@n ews02.tsnz.net> ...
Chris Mantoulidis wrote:
So, <class T> allows ANY KINDA class and type (int, char, etc.) to be
passed as the template whereas <template T> allows just types?? Is
that it? Cuz I'm a bit confused.
No, the two are equivalent. You can't restrict T, but you can use T in
such a way that it only compiles with compatible types.

compatible types = int, char, etc. ???


Compatible types == ones supporting the required operations (assignment,
default construction, etc). There's no difference between fundamental
types (int, char, etc) and user-defined types from a template's point of
view.


Then what's the difference between typename and class... You don't really explain


I said the two are equivalent. There is no difference.

Jacques.

Jul 22 '05 #9
In article <vo************ ********@news02 .tsnz.net>,
Jacques Labuschagne <ja*****@clawsh rimp.com> wrote:
You can't restrict T, but you can use T in
such a way that it only compiles with compatible types.


You can also restrict T for the purpose of guiding overload resolution
(though it is far from clear that that is what the OP needed). Search
for "restrict_t o" and "enable_if" .

-Howard
Jul 22 '05 #10

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

Similar topics

6
11524
by: Suzanne | last post by:
Hi++, I get a link error when I try to call a template function that is declared in a header file and defined in a non-header file. I do *not* get this link error if I define the template function directly in the header file, or if the change the function to non-template. For example... *** Why am I getting a link error for template...
3
1617
by: Dennis Pais | last post by:
Here's a piece of code that I found in C++ Primer by Stan Lippman ! ------------------------------------------------- template<typename T, int size> T min(T (&r_array) ) { Type min_value = r_array; for(int i=1; i<size; i++) {
5
5278
by: nifsmith | last post by:
Hi I am trying to learn about Queues and use templates at the same time. I have written the following code and I am getting a link error, stating "unresolved external symbol, "int__cdecl adsq::QInitialise<struct adsq::Data>(struct adsq::Head<struct adsq::Data> *)" -----------adsq.h file
5
6569
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
16
16208
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
6
2750
by: wkaras | last post by:
I tried a couple of compilers, and both gave errors compiling this: template <bool fin, typename T> T foo(T val); template <typename T> T foo<true, T>(T val) { return(val); } But both gave no errors compiling this:
2
1574
by: recover | last post by:
#include <stdio.h> template<class T> class TpHello { public: int GetHash(){return 0;} protected: private: T a;
5
1636
by: desktop | last post by:
I have this example: template<class T(1) void f( T ); template<class T(2) void f( T* ); template< (3)
4
2756
by: nickyspace | last post by:
HI all I have a little issue with this php code. Below is the code CODE: PHP 1.<?
8
284
by: William Xu | last post by:
Compiling: template <class T = int> T foo(const T& t) {} int main(int argc, char *argv) {} gcc complains:
0
7475
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7921
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7437
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...
0
7771
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5982
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...
1
5343
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...
0
4958
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...
1
1900
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
1
1023
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.