473,490 Members | 2,472 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Explicit Template Argument Specification

6 New Member
i started reading about templates and i got confused on the below.
Expand|Select|Wrap|Line Numbers
  1. template<class T>
  2. T max(T t1, T t2)
  3. {
  4.    if (t1 > t2)
  5.       return t1;
  6.    return t2;
  7. }
  8.  

int main(){

std::cout<<max<int>120,14.55);//=>as per my understanding here if i explicitly mention only one data type,other would be deducted from the argument type(14.55) by the compiler.That means T max(T t1, T t2) instantiates T max(int t1,double t2)in this case.But when i compiled it i got the below warning


warning: passing double for argument 2 to T max(T, T) [with T = int].Then why this warning came is my question since i have already instantiated for T max(int t1,double t2).

also since we are using only T,if i mention max<int>,then T max(T t1, T t2) should be like this int max(int t1, int t2) na??

return 0;

}

Please clear my doubts.else i cant proceed further
Dec 4 '12 #1
1 2045
weaknessforcats
9,208 Recognized Expert Moderator Expert
Your template only uses T as a parameter. That means you can't use int and double.

Since the template only returns T you can't return int or double.

A template is only a pattern. When you specialize the template, the compiler makes a copy of it and replaces the T with the type you specify. This creates the function that will be called.

You use explicit specialization then certain types require different logic:

Expand|Select|Wrap|Line Numbers
  1. template <class T>
  2. bool IsZero(T arg)
  3. {
  4.      if (arg) return true;
  5.      return false;
  6. }
  7. //explicit 
  8. //do not use the template when T is double
  9. bool IsZero(double arg)
  10. {
  11.     if (fabs(arg) < 0.0001) return false;
  12.     return true;
  13. }
Ths tells the compiler to not use the template if T is double.

Since bool IsZero(double arg) is an actual function, it will not be in a header file but will be in an implementation file somewhere in the build. Therefore, you need to tip off the compiler that the function exists. You do it this way:

Expand|Select|Wrap|Line Numbers
  1. template <class T>
  2. bool IsZero(T arg)
  3. {
  4.      if (arg) return true;
  5.      return false;
  6. }
  7. //explicit specialization
  8. //do not use the template when T is double
  9. template<> bool IsZero(double arg);
  10. }
Dec 4 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
6173
by: wogston | last post by:
A) template <typename scalar, int size> struct basevector { enum { size = size }; scalar v; }; B)
1
1568
by: J.T. Conklin | last post by:
I've been working on the configuration infrastructure for the ACE framework which contains code that uses a templatized handle/body idiom with placement new/delete. With g++ 3.4, we found that the...
8
11338
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...
3
1902
by: BigMan | last post by:
Here is a piece of code: #include <memory> using namespace std; template< typename SomeType > void f(auto_ptr_ref< SomeType >) { }
3
2426
by: stain | last post by:
hi, the following code produces an "illegal explicit template specialization" warning. I have been fiddling around a while to find a solution to specialize a template method of a specialized...
4
1628
by: Fei Liu | last post by:
#include <vector> #include <iostream> template <template <typename T, typename Allocclass C> struct A{ //C<Tc; //A(){ c=10; } void print(){ std::cout << "A" << std::endl; } };
7
12569
by: gretean | last post by:
I have a problem that's driving me crazy involving Microsoft's ability to deduce template parameters. I am using Visual Studio .NET (aka VC7?), and it gives an error compiling the following code....
2
1769
by: montyshasta | last post by:
Take this code as a base case, it compiles successfully: struct R { int i; }; class S : public R { void F(void) {i = 0;} };
1
5404
by: subramanian100in | last post by:
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...
9
17489
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
6974
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...
0
7146
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,...
0
7356
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...
0
5448
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,...
0
4573
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...
0
3084
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...
0
1389
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 ...
1
628
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
277
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.