I'd like to create a partial specialization of a member-method of a
parameterized class which takes a parameterized argument, but I'm not
sure if it's possible or, if possible, how. The following code
demonstrates what I'd like to to:
#include <iostream>
template<class T>
struct BarA
{
void bar() { std::cout << "BarA\n"; }
};
template<class T>
struct BarB
{
void bar() { std::cout << "BarB\n"; }
};
template<class T>
struct Test
{
template<template<class U =3D T> class V>
void foo(V<T>& f);
};
template<class T>
template<template<class> class V>
void Test<T>::foo(V<T>& f)
{
f.bar();
}
/* A partial specialization that prepends the output of f.bar() with
"BarB: "
template<class T>
template<>
void Test<T>::foo(BarB<T> f)
{
std::cout "BarB: ";
f.bar();
}
*/
int main()
{
BarA<int> a;
BarB<int> b;
Test<int> t;
t.foo(a);
t.foo(b);
}
This should, print:
BarA
BarB: BarB
Any help appreciated.
--
Erik Wikstr=F6m
|
|
0
|
|
|
|
Reply
|
eriwik (500)
|
12/20/2006 1:00:45 PM |
|
Erik Wikstr=F6m wrote:
> template<class T>
> struct Test
> {
> template<template<class U =3D T> class V>
> void foo(V<T>& f);
void foo(BarB<T> &f);
> };
>
> template<class T>
> template<template<class> class V>
> void Test<T>::foo(V<T>& f)
> {
> f.bar();
>
> }
>
template<class T>
void Test<T>::foo(BarB<T> & f)
{
std::cout << "BarB: ";
f.bar();
}
|
|
0
|
|
|
|
Reply
|
dasjotre (181)
|
12/20/2006 1:54:08 PM
|
|
On Dec 20, 2:54 pm, "dasjotre" <dasjo...@googlemail.com> wrote:
> Erik Wikstr=F6m wrote:
> > template<class T>
> > struct Test
> > {
> > template<template<class U =3D T> class V>
> > void foo(V<T>& f); void foo(BarB<T> &f);
>
> > };
>
> > template<class T>
> > template<template<class> class V>
> > void Test<T>::foo(V<T>& f)
> > {
> > f.bar();
>
> > }template<class T>
> void Test<T>::foo(BarB<T> & f)
> {
> std::cout << "BarB: ";
> f.bar();
> }
Yes, of course. Thanks.
--
Erik Wikstr=F6m
|
|
0
|
|
|
|
Reply
|
eriwik (500)
|
12/20/2006 1:59:36 PM
|
|
Erik Wikstr�m wrote:
> I'd like to create a partial specialization of a member-method [..]
You can stop right there. C++ does not allow partial specialisations
of function templates. Another mistake: you cannot specialise any
member without first specialising the class template.
The specification you gave in the form of your [non-compiling] code
is a bit unclear. Are you trying to make your 'foo' member have
a different behaviour if the template for which it's instantiated is
'BarB' (versus any other)? It is very likely you need a helper class
template for that. You can then specialise your helper template:
template<class T> struct TestHelper {
static void help() {} // do nothing for all
};
template<class T> sturct TestHelper<BarB<T> > {
static void help() { std::cout << "BarB: "; }
};
template<class T> struct Test {
template<template<class> class B> void foo(B<T> f) {
TestHelper<B<T> >::help(); ///////////////////// some help
f.bar();
}
};
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
|
|
0
|
|
|
|
Reply
|
v.Abazarov (13255)
|
12/20/2006 2:05:29 PM
|
|