473,395 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Partial specialisation of method taking parameterized parameter

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
3 1195

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section...
5
by: sks_cpp | last post by:
template< class a, class b, class c = int > struct something { }; template< class a, class b > struct something<a, b> { };
7
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
5
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
1
by: Alfonso Morra | last post by:
if I have a class template declared as ff: (BTW is this a partial specialization? - I think it is) template <typename T1, myenum_1 e1=OK, my_enum_2=NONE> class A { public: A(); virtual...
6
by: rincewind | last post by:
Hi, can anybody summarise all options for partial template specialization, for all kind of parameters (type, nontype, template)? I *think* I understand options for partial specialization on...
1
by: aaragon | last post by:
Hi everyone, I've been trying to compile a very simple code for template partial specializatoin. I can't compile this code! what is wrong with it? I'm using the g++ compiler. #include...
4
by: Alan Woodland | last post by:
Hi, The following code is legal, and works as expected: #include <iostream> template <typename T> class Bar { };
1
by: Martin | last post by:
I'm trying to make a partial specialization of a class of mine. Can someone please tell me what's wrong with the following code? GCC gives me the error "invalid use of undefined type "class X<int,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.