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

Partial specialisation of method taking parameterized parameter

P: n/a
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 = Tclass V>
void foo(V<T>& f);
};

template<class T>
template<template<classclass 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<Tf)
{
std::cout "BarB: ";
f.bar();
}
*/

int main()
{
BarA<inta;
BarB<intb;

Test<intt;
t.foo(a);
t.foo(b);
}

This should, print:
BarA
BarB: BarB

Any help appreciated.

--
Erik Wikström

Dec 20 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a

Erik Wikström wrote:
template<class T>
struct Test
{
template<template<class U = Tclass V>
void foo(V<T>& f);
void foo(BarB<T&f);
};

template<class T>
template<template<classclass 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();
}

Dec 20 '06 #2

P: n/a
On Dec 20, 2:54 pm, "dasjotre" <dasjo...@googlemail.comwrote:
Erik Wikström wrote:
template<class T>
struct Test
{
template<template<class U = Tclass V>
void foo(V<T>& f); void foo(BarB<T&f);
};
template<class T>
template<template<classclass 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öm

Dec 20 '06 #3

P: n/a
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 Tstruct TestHelper {
static void help() {} // do nothing for all
};

template<class Tsturct TestHelper<BarB<T {
static void help() { std::cout << "BarB: "; }
};

template<class Tstruct Test {
template<template<classclass Bvoid foo(B<Tf) {
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
Dec 20 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.