Re: partial template specialization
cpunerd@gmail.com wrote:
On Apr 30, 8:21 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
cpun...@gmail.com wrote:
I'm confused as to the syntax for partial template specialization.
Below is a very small example. Can someone tell me what I should do
to make this compile properly on an ISO-compliant compiler?
template <typename T1, typename T2>
class A
{
public:
T1 t1;
T2 t2;
};
template <typename T1, typename T2>
class B
{
public:
void insert(A<T1, T2> f);
};
template <typename T1>
void B<T1, int>::insert(A<T1, int> f)
{
f.t1 = 0;
f.t2 = 0;
}
You've attempted to partially specialise a member function. That's
not allowed. You may partially specialise the whole class only.
Besides, I can't see why you'd want to do that. Both functions
are exactly the same.
template <typename T1, typename T2>
void B<T1, T2>::insert(A<T1, T2> f)
{
f.t1 = 0;
f.t2 = 0;
}
int main()
{
A<int, int> a1;
A<float, float> a2;
B<int, int> b1;
B<float, float> b2;
b1.insert(a1);
b2.insert(a2);
return 0;
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
in this case both functions are exactly the same. it is simply a
trivial example to show the problem, which is as you said, you cant
partially specialize a member function. the instance in where i am
trying to do something like this is indeed useful, pity... thanks for
the help though. is there a better idea that you can think of than
making a friend function and specializing the friend function?
One approach:
--------------------------
template<class T, class U> struct A
{
};
template<class U> struct NotInt { enum { yes = 1 }; };
template<> struct NotInt<int> { enum { yes = 0 }; };
template<class T, class U> struct B
{
void foo_generic(A<T,U>& a)
{
cout << "Generic\n";
}
void foo_int(A<T,U>& a)
{
cout << "Specialised\n";
}
void foo(A<T,U>& a)
{
if (NotInt<U>::yes)
foo_generic(a);
else
foo_int(a);
}
};
--------------------------
Another approach:
--------------------------
template<class T, class U> struct A
{
};
template<class U> struct NotInt { static const size_t s = 1; };
template<> struct NotInt<int> { static const size_t s = 0; };
template<class T, class U> struct B
{
template<size_t S>
void foo_helper(A<T,U>& a, int (*)[S] = 0) // OK for non-int 'U'
{
cout << "Generic\n";
}
template<size_t S>
void foo_helper(A<T,U>& a, int (*)[1-S] = 0) // OK only for int 'U'
{
cout << "Specialised\n";
}
void foo(A<T,U>& a)
{
foo_helper<NotInt<U>::s>(a);
}
};
--------------------------
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask