Function template partial specialization

L

lhr_cool_guy

C++ doesn't allow partial specialization of function templates. I want
to know why this is so? Is the concept of function template partial
specialization not well defined? I have a case where I feel there is a
genuine need for it. Please take a look and tell me if there is a
workaround:

template< class T, class allocator >
class A
{
/* ... */
public:
/* ... */
template< class U, class B >
A( const A< U, B > & ref ) { /* ... */ } // General copy constructor
/* Now I want to write a special copy constructor for A< T, B>, i.e.,
the first function template parameter is the same as the first template
parameter of class A, while the second is any general parameter. How do
I do this? */
};
 
V

Victor Bazarov

C++ doesn't allow partial specialization of function templates. I want
to know why this is so? [..]

'comp.std.c++' might be a better place to ask.

IIRC, the reason was to simplify the task for compiler developers. The
same effect can be achieved with overloading.

V
 
N

Neelesh Bodas

template< class T, class allocator >
class A
{
/* ... */
public:
/* ... */
template< class U, class B >
A( const A< U, B > & ref ) { /* ... */ } // General copy constructor
/* Now I want to write a special copy constructor for A< T, B>, i.e.,
the first function template parameter is the same as the first template
parameter of class A, while the second is any general parameter. How do
I do this? */

You can write another Constructor to do this job. Following is a
complete working example.

#include <iostream>
using std::cout;
using std::endl;

template <class T, class Z> class A
{
public:
template <class U, class B> A(const A<U,B>& ref)
{
cout << "Double variation" <<endl;
}

template<class B> A(const A<T,B>& ref)
{
cout << "Single variation" << endl;
}

A()
{
}

};

int main()
{
A<int,char> a;
A<int,bool> a2(a); // calls the single variation version
A<double,bool> a3(a); // calls the double variation version
}
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top