473,651 Members | 2,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<templa te<class U = Tclass V>
void foo(V<T>& f);
};

template<class T>
template<templa te<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(Ba rB<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 1212

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

template<class T>
template<templa te<classclass V>
void Test<T>::foo(V< T>& f)
{
f.bar();

}
template<class T>
void Test<T>::foo(Ba rB<T& f)
{
std::cout << "BarB: ";
f.bar();
}

Dec 20 '06 #2
On Dec 20, 2:54 pm, "dasjotre" <dasjo...@googl email.comwrote:
Erik Wikström wrote:
template<class T>
struct Test
{
template<templa te<class U = Tclass V>
void foo(V<T>& f); void foo(BarB<T&f);
};
template<class T>
template<templa te<classclass V>
void Test<T>::foo(V< T>& f)
{
f.bar();
}template<class T>
void Test<T>::foo(Ba rB<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<templa te<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
6849
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 18.3.1, 'Iseq'). I want the version for containers that he gives, but also to provide a specialization for construction from a pair<It,It> (eg because that is returned by equal_range()).
5
1683
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
2124
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
6578
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
2265
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 ~A() ;
6
2804
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 type parameters - in place of a template argument one can construct arbitrary valid C++ type declaration, more or less like in "typedef" statement. What about nontype parameters? Am I right that you cannot partially
1
1318
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 <vector> #include <iostream> using namespace std; template <class T, class U>
4
1354
by: Alan Woodland | last post by:
Hi, The following code is legal, and works as expected: #include <iostream> template <typename T> class Bar { };
1
1744
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, B>"". template <class A, class B> class X { public: void f(); };
0
8352
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8697
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8465
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7297
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5612
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2699
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1909
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1587
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.