Error : in Template Code

P

Pallav singh

Hi All,

i am getting error during explicit function Instantiation for a class
Template
if i do explicit Instantiation of class it work and all function
symbol i get in object file
But if i try to expose only one function of my class its failing

#include <iostream>

template<typename T>
class A
{
const T max(const T a,const T b)
{ return ((a<b)?b:a); }

const T min(const T a,const T b)
{ return ((a<b)?a:b); }
};

// used for Explicit Instantiation of class
//template class A<int>;

// used for Explicit Instantiation of different function of class
template int A<int>::max(int,int);
template int A<int>::min(int,int);

Thanks
Pallav Singh
 
T

tatmasi

Hi All,

i am getting  error during explicit function Instantiation for a class
Template
if i do explicit Instantiation of class it work and all function
symbol i get in object file
But if i try to expose only one function of my class its failing

#include <iostream>

template<typename T>
class A
{
   const T  max(const T  a,const T  b)
     {  return ((a<b)?b:a);  }

   const T  min(const T  a,const T  b)
    {  return ((a<b)?a:b);  }

};

// used for Explicit Instantiation of class
//template class A<int>;

// used for Explicit Instantiation of different function of class
template int A<int>::max(int,int);
template int A<int>::min(int,int);

Thanks
Pallav Singh

Hi,
I could not understand why you need explicit function templates in
this context.
Since the explicit instantiation of class allows you to use functions
correctly.
By explicit declaration functions as you done will result error
because when a function template
is used in a way that triggers its instantiation, a compiler will (at
some point) need to
see that template's definition. This breaks the usual compile and
link distinction for
ordinary functions, when the declaration of a function is sufficient
to compile its use.
 
P

Pallav singh

Hi,
I could not understand why you need explicit function templates in
this context.
Since the explicit instantiation of class allows you to use functions
correctly.
By explicit declaration functions as you done will result error
because when a function template
 is used in a way that triggers its instantiation, a compiler will (at
some point) need to
 see that template's definition. This breaks the usual compile and
link distinction for
 ordinary functions, when the declaration of a function is sufficient
to compile its use.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Hi

But if i write code in following way then also i get Error
during Function Template Instantiation

Kindly let me know How to make it work ??

#include <iostream>


template<typename T>
const T max(const T a, const T b)
{ return ((a<b)?b:a); }


template<typename T>
const T min(const T a, const T b)
{ return ((a<b)?a:b); }


template int max(int,int);
template int min(int,int);
 
L

Lars Tetzlaff

Pallav said:
Hi

But if i write code in following way then also i get Error
during Function Template Instantiation

Kindly let me know How to make it work ??

#include <iostream>


template<typename T>
const T max(const T a, const T b)
{ return ((a<b)?b:a); }


template<typename T>
const T min(const T a, const T b)
{ return ((a<b)?a:b); }


template int max(int,int);
template int min(int,int);

1) Wy don't you post the error message? How should we know *WHAT* error
you get?

2) do you mean:

#include <iostream>


template<typename T>
const T max(const T a, const T b)
{ return ((a<b)?b:a); }


template<typename T>
const T min(const T a, const T b)
{ return ((a<b)?a:b); }


template<> const int max(const int, const int);
template<> const int min(const int, const int);



Lars
 
I

Ian Collins

Pallav said:
Hi

But if i write code in following way then also i get Error
during Function Template Instantiation

Kindly let me know How to make it work ??

#include <iostream>


template<typename T>
const T max(const T a, const T b)
{ return ((a<b)?b:a); }


template<typename T>
const T min(const T a, const T b)
{ return ((a<b)?a:b); }


template int max(int,int);
template int min(int,int);

Did you read my replay the last time you asked this?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top