By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

Function template partial specialization

P: n/a
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? */
};

Dec 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

lh**********@yahoo.com wrote:

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
}

Dec 19 '05 #2

P: n/a
lh**********@yahoo.com wrote:
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
Dec 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.