How to determine the type of template arguments

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 8622 Views - Last Post: 18 August 2010 - 04:07 PM Rate Topic: -----

#1 jimblumberg  Icon User is online

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,678
  • Joined: 25-December 09

How to determine the type of template arguments

Posted 18 August 2010 - 08:33 AM

When using a template class is it possible to determine what type the arguments are, inside the template class
template<class T>
T send( T &b)
{
   // do something if b is a certain type

   // do something else

}




Jim
Is This A Good Question/Topic? 0
  • +

Replies To: How to determine the type of template arguments

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: How to determine the type of template arguments

Posted 18 August 2010 - 08:44 AM

This is why we use template specialization
template<class T>T send( T &B)/> //default template
{
   return b;
}

template<>int send<int>(int &B)/> //case for ints
{
   return b;
}
template<>char send<char>(char &B)/> //case for chars
{
   return b;
}

This post has been edited by ImaSexy: 18 August 2010 - 08:45 AM

Was This Post Helpful? 0
  • +
  • -

#3 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: How to determine the type of template arguments

Posted 18 August 2010 - 08:45 AM

using the sizeof function would give you some idea, for example.

if (sizeof(val) == 4)
    // this is an integer
else if (sizeof(val) == 2)
    // this is a short
//etc


this is far from fool proof though of course.

EDIT: ImaSexy's answer is far better.

This post has been edited by Aphex19: 18 August 2010 - 08:45 AM

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,678
  • Joined: 25-December 09

Re: How to determine the type of template arguments

Posted 18 August 2010 - 09:15 AM

View PostImaSexy, on 18 August 2010 - 07:44 AM, said:

This is why we use template specialization
template<class T>T send( T &B)/> //default template
{
   return b;
}

template<>int send<int>(int &B)/> //case for ints
{
   return b;
}
template<>char send<char>(char &B)/> //case for chars
{
   return b;
}



I tried the above and get the following errors

error: explicit specialization in non-namespace scope ‘class SerialUnit’|
error: template-id ‘send<std::string>’ in declaration of primary template|

class SerialUnit : public Serial_Base
{
   public:
      template <class T>
      T send(std::string a , T &B)/>
      { 
         return(B)/>;
      }
      template<>std::string send<std::string>( std::string a,std::string &B)/>
      {
         return(B)/>;
      }

};




Jim
Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: How to determine the type of template arguments

Posted 18 August 2010 - 09:29 AM

compiles fine for me, visual studio 2008
class SerialUnit// : public Serial_Base
{
   public:
      template <class T> T send(std::string a , T &B)/>
      { 
         return(B)/>;
      }
      template<> std::string send<std::string>(std::string a, std::string &B)/>
      {
         return(B)/>;
      }

};

int main()
{
      std::string a = "a", c = "c";
	  int d = 100;
	  std::cout<<send(a,c)<<std::endl
	           <<send(a,d)<<std::endl;
			   
      return 0;
}



This post has been edited by ImaSexy: 18 August 2010 - 09:30 AM

Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,678
  • Joined: 25-December 09

Re: How to determine the type of template arguments

Posted 18 August 2010 - 10:27 AM

I am compiling on Linux using gcc 4.4.4. and I get the errors mentioned.
Was This Post Helpful? 0
  • +
  • -

#7 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: How to determine the type of template arguments

Posted 18 August 2010 - 01:11 PM

isn't gcc a C compiler? and g++ a c++ compiler? Can someone else try an compile that to give us confermation?

This post has been edited by ImaSexy: 18 August 2010 - 01:12 PM

Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,678
  • Joined: 25-December 09

Re: How to determine the type of template arguments

Posted 18 August 2010 - 01:28 PM

Yes gcc is a c compilier. I meant g++ version 4.4.4.

I have it working by overloading the send() for std::string.


#include <iostream>
#include <string>

class SerialUnit// : public Serial_Base
{
   public:
      template <class T> T send(std::string a , T &B)/>
      {
         return(B)/>;
      }
      
      std::string send(const std::string a, std::string &B)/>
      { // This is to process for a string only.
         return(B)/>;
      }

};

int main()
{
   SerialUnit test;
   std::string a = "a", c = "c";
   int d = 100;
   std::cout<< test.send(a,c) << std::endl
            << test.send(a,d) << std::endl;

   return 0;
}





Jim
Was This Post Helpful? 0
  • +
  • -

#9 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: How to determine the type of template arguments

Posted 18 August 2010 - 01:37 PM

well it still doesnt make sense whey you cannot specialize the template under g++. But im glad you found an answer :)
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is online

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,678
  • Joined: 25-December 09

Re: How to determine the type of template arguments

Posted 18 August 2010 - 01:46 PM

If I remove the template from the class then your code works.

#include <iostream>
#include <string>

//class SerialUnit// : public Serial_Base
//{
 //  public:
      template <class T> T send(std::string a , T &B)/>
      {
         return(B)/>;
      }
      template<>std::string send<std::string>( std::string a,std::string &B)/>
      {
         return(B)/>;
      }
/*
      std::string send(const std::string a, std::string &B)/>
      { // This is to process for a string only.
         return(B)/>;
      }
*/
//};

int main()
{
  // SerialUnit test;
   std::string a = "a", c = "c";
   int d = 100;
   std::cout<< send(a,c) << std::endl
            << send(a,d) << std::endl;

   return 0;
}






Jim
Was This Post Helpful? 0
  • +
  • -

#11 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,339
  • Joined: 20-August 07

Re: How to determine the type of template arguments

Posted 18 August 2010 - 01:48 PM

Template member function specialisations must be defined outside of the class body, and not inline. Always keep in mind that a template function is merely a template and not a concrete function (i.e. it doesn't 'exist' until its actually used in your code), Your compiler probably thinks that you're providing multiple declarations of the same template within your class.
#include <iostream>
#include <string>

class SerialUnit
{
   public:
      template <class T> 
      std::string send(std::string, T&);
};

template <class T>
std::string SerialUnit::send(std::string, T&)
{
    return "Send<T>";
}

template<>
std::string SerialUnit::send<std::string>(const std::string, std::string&)
{ 
    return "Send<std::string>";
}

int main()
{
   SerialUnit test;
   std::string a = "a", c = "c";
   int d = 100;
   std::cout<< test.send(a,c) << std::endl
            << test.send(a,d) << std::endl;
}

This post has been edited by Bench: 18 August 2010 - 01:57 PM

Was This Post Helpful? 2
  • +
  • -

#12 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: How to determine the type of template arguments

Posted 18 August 2010 - 01:49 PM

Ok.. i figured it out lol explicit specializations have to be within namespace scope which the class belongs too.

Quote

An explicit specialization shall be declared in the namespace of which the template is a member, or, for member templates, in the namespace of which the enclosing class or enclosing class template is a member.
An explicit specialization of a member function, member class or static data member of a class template shall be declared in the namespace of which the class template is a member.


Heres the fix
#include <iostream>
#include <string>

class SerialUnit// : public Serial_Base
{
   public:
      template <class T> T send(std::string a , T &B)/>;
};

template <class T> T SerialUnit::send(std::string a , T &B)/>
{
     return B;
}
template <> std::string SerialUnit::send<std::string>(std::string a , std::string &B)/>
{
     return B;
}

int main()
{
   SerialUnit test;
   std::string a = "a", c = "c";
   int d = 100;
   std::cout<< test.send(a,c) << std::endl
            << test.send(a,d) << std::endl;

   return 0;
}




EDIT: ninja'd :(

This post has been edited by ImaSexy: 18 August 2010 - 01:51 PM

Was This Post Helpful? 2
  • +
  • -

#13 jimblumberg  Icon User is online

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,678
  • Joined: 25-December 09

Re: How to determine the type of template arguments

Posted 18 August 2010 - 01:54 PM

Thanks Imasexy and Bench. That answers my question and solves my problems.

Jim
Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is online

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,678
  • Joined: 25-December 09

Re: How to determine the type of template arguments

Posted 18 August 2010 - 03:22 PM

Another question, is it acceptable to put the explicit specializations in the include file or should they be in a .cpp file?

Jim
Was This Post Helpful? 0
  • +
  • -

#15 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: How to determine the type of template arguments

Posted 18 August 2010 - 03:28 PM

The down side about templates is that they must be in the include file or else you will recieve linking errors. It is possible to put template definitions in the cpp file but you have to use Template Instantiation.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2