473,614 Members | 2,351 Online
Bytes | Software Development & Data Engineering Community
+ 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 2065
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
6183
by: wogston | last post by:
A) template <typename scalar, int size> struct basevector { enum { size = size }; scalar v; }; B)
1
1576
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 delete needed the addition of a template argument that wasn't needed in earlier versions. I've checked this with Comeau C++ Online in strict mode, and with Gimpel's Flexelint and neither reported any issues with the code without the template...
8
11357
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)
3
1922
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
2447
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 template class. Here is what I would like to be able to call: ChannelIndexTraits<RGB>::index<RED>() == 0 ChannelIndexTraits<RGB>::index<GREEN>() == 1 ChannelIndexTraits<RGB>::index<ALPHA>() == -1
4
1641
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
12578
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. template <int x, int yclass M { public: template <int xRet, int yRet, int xR, int yR> M<xRet, yRetoperator *(const M<xR, yR&b) const { M<xRet,yReta; return a;
2
1773
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
5442
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 exists
9
17524
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
8197
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8640
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...
0
8589
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
8287
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
8443
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
7114
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
6093
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
5548
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
4058
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...

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.