473,714 Members | 2,531 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Template function question

Suppose you have

class A {};
class B {}
template<class T> void SomeFcn( T t ) {…};

A a;
B b;

These are both legal:

SomeFcn( a );
SomeFcn<B>( b );

The question is: When (if ever) is the second syntax required?

// Microsoft MFC specific version of question
template<class T> void SomeArrayFcn( CArray< T, T>& array ) {…};

CArray<A,A> arraya;
CArray<B,B> arrayb;

These are both legal:

SomeArrayFcn( arraya);
SomeArrayFcn<B> ( arrayb);

The question is: When (if ever) is the second syntax required?
May 3 '06 #1
5 1754
Steve Schlesinger wrote:
Suppose you have

class A {};
class B {}
template<class T> void SomeFcn( T t ) {...};

A a;
B b;

These are both legal:

SomeFcn( a );
SomeFcn<B>( b );

The question is: When (if ever) is the second syntax required?
I don't foresee a case in which the explict type syntax would ever be
needed in order to call SomeFcn, since the compiler can always deduce a
paramterized type from the type of the parameter passed. Of course, the
client may wish to call a different implementation of SomeFcn than the
one the compiler selects. In that case the client would have to specify
the parameterized type explicitly. But I don't foresee the syntax being
necessary, as it would, say, with a function template that accepted no
parameters.
// Microsoft MFC specific version of question
template<class T> void SomeArrayFcn( CArray< T, T>& array ) {...};

CArray<A,A> arraya;
CArray<B,B> arrayb;

These are both legal:

SomeArrayFcn( arraya);
SomeArrayFcn<B> ( arrayb);

The question is: When (if ever) is the second syntax required?


Again, the second syntax should never be required for this example to
compile. And again, the syntax would still be needed if the client is
not happy with the compiler's type deduction and wishes to override it.
Greg

May 3 '06 #2
"Steve Schlesinger" <ss**********@c ox.net> wrote in message
news:nxW5g.3022 $_m5.232@fed1re ad09...
: Suppose you have
....
: template<class T> void SomeFcn( T t ) {…};
....
: These are both legal:
:
: SomeFcn( a );
: SomeFcn<B>( b );
:
: The question is: When (if ever) is the second syntax required?

Sometimes the template parameters cannot be deduced from the function
call arguments (e.g. it is a return value, or internally used).

It might also be needed to resolve ambiguities.
Consider:
template<class T> T max(T a, T b);
sample uses:
double v;
std::cin >> v;
v = max( v, 0 ); // --> Error
v = max<double>( v, 0 ); // ok
v = max( v, 0.0 ); // ok

The notation is also handy when wanting to take the address of
the function:
myFuncPtr = & max<double>;

....

hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com


May 3 '06 #3
> SomeFcn<B>( b );

The question is: When (if ever) is the second syntax required?


Since argument deduction doesn't do any automatic type conversion,
the 2nd syntax will be needed when you want type conversion. (either to
resolve ambiguity or for "conversion ")

example :
template<typena me T>
void f(T t){cout << t << endl; }
int main()
{
f<int>('a'); // shows you 97
f('a'); // shows you a
}

regards,
Aman Angrish.
--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
May 3 '06 #4
Steve Schlesinger wrote:
Suppose you have

class A {};
class B {}
template<class T> void SomeFcn( T t ) {…};

A a;
B b;

These are both legal:

SomeFcn( a );
SomeFcn<B>( b );

The question is: When (if ever) is the second syntax required?


As Ivan has already mentioned, it is necessary for functions that take
no arguments, or only such arguments from which the template type can't
be deduced. While such functions should be pretty rare (I'd be curious
if anybody knows such a case), you will encounter the explicit
parametrisation when you invoke member functions on objects. One case
where such explicit template parameters are necessary are cast operators
(they usually don't have arguments, so the compiler needs your advice
for deciding which member function to generate).

Regards,
Stuart
May 3 '06 #5
Thanks to all who responded -

I assume it is safe to say then, that if the compiler required a class
specifier
eg SomeFcn<B>( b );
and I didn't have one, it would flag it as an error.

-Steve
"Stuart Redmann" <De*******@web. de> wrote in message
news:e3******** **@news.dtag.de ...
Steve Schlesinger wrote:
Suppose you have

class A {};
class B {}
template<class T> void SomeFcn( T t ) {…};

A a;
B b;

These are both legal:

SomeFcn( a );
SomeFcn<B>( b );

The question is: When (if ever) is the second syntax required?


As Ivan has already mentioned, it is necessary for functions that take
no arguments, or only such arguments from which the template type can't
be deduced. While such functions should be pretty rare (I'd be curious
if anybody knows such a case), you will encounter the explicit
parametrisation when you invoke member functions on objects. One case
where such explicit template parameters are necessary are cast operators
(they usually don't have arguments, so the compiler needs your advice
for deciding which member function to generate).

Regards,
Stuart

May 4 '06 #6

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

Similar topics

6
2016
by: Dave | last post by:
Hello all, Consider this function template definition: template<typename T> void foo(T) {} If foo is never called, this template will never be instantiated. Now consider this explicit instantiation of foo:
3
2093
by: 胡岳偉(Yueh-Wei Hu) | last post by:
Hi all, I have 2 questions about template function as friends in template classes. I don't know why, and hope someone could help me. ============================================================== Question 1: ============================================================== Compile the following codes, and run it.
0
1694
by: Yueh-Wei Hu | last post by:
Victor Bazarov <v.Abazarov@comAcast.net> wrote in message news: ============================================================== > > Question 1: > > ============================================================== > Your code refuses to compile. In function 'foo<a>' 't' is a pointer to > const. Friendship has nothing to do with it. When I remove the attempt > to change 't->c' in foo<a>, it compiles and runs and displays > > T<a, 3>
4
2341
by: Thomi Richards | last post by:
Hi, I'm trying to create a simple stack class using C++ and templates. Everything works well and good if I amke the class totally inline. However, as soon as I try to seperate the class into a .h and a .cpp file, gcc throws some strange linking errors. Compiling like this:
5
1225
by: wongjoekmeu | last post by:
Hello all, I have a question about templates. Let say I have the following template function ----- template <class T> T max(T a, T b) { return a > b ? a : b ; }
2
2314
by: pookiebearbottom | last post by:
Just trying to learn some things about templates. Was wondering how boost::tupple really works, but the headers were a bit confusing to me. I know you get do something like the following, just want to know how it works with the overloading of get<>(). boost::tupple<int,doubletup(1,2.0); double d=tup.get<2>(); // equal 2.0 // simple set up,
5
2267
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer to functions are hard to inline for the compiler, and so you won't be able to avoid function call overhead. This is an important aspect when you are calling a function very frequently for evaluation reason: think of the problem of performing...
6
1722
by: Bartholomew Simpson | last post by:
I'm trying to avoid (or at least, minimize) duplicity of effort. I have the following function: void Permissions::Exists(const unsigned int id, std::vector<Permission>::const_iterator& iter) { if (m_permissions.empty()) iter = m_permissions.end() ; std::vector<Permission>::const_iterator cit=m_permissions.begin();
4
3014
by: David Sanders | last post by:
Hi, I have a class with an integer template parameter, taking values 1, 2 or 3, and a function 'calc' in that class which performs calculations. Some calculations need only be performed if the template parameter is 2 or 3; for efficiency, I do not wish to perform the calculations if the template parameter is 1. I currently do this as follows:
21
4737
by: H9XLrv5oXVNvHiUI | last post by:
Hi, I have a question about injecting friend functions within template classes. My question is specific to gcc (version 3.4.5) used in combination with mingw because this code (or at least code that gets the same result) works as expected in visualc++. I know that this is probably not the right behavior for a compiler but it's the kind of behavior I'm searching for so I was hoping there was a way to do the same thing in gcc. As you know...
0
8704
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
9170
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9071
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
9009
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7946
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
6627
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
4462
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
3155
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
2514
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.