473,326 Members | 2,102 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,326 software developers and data experts.

Template specialization with the parameter being a template

Hi, I need to specialize one operation in a template class, when the
parameter is a template itself.
Suppose this code:

#include <string>

template <typename T>
struct Nullable
{};

template <typename T>
class A
{
public:
void fn(const std::string& s)
{}
};

template<>
void A<int>::fn(const std::string& s)
{
// this is supposed to be A::fn() specialization for type int
}

int main()
{
A<inta;
a.fn(""); // ok, invokes the specialized fn()
A<charach;
ach.fn(""); // ok, invokes the generic fn()
A<Nullable<int an;
an.fn(""); // ok, invokes the generic fn()
return 0;
}

I need to specialize A::fn() for Nullable<>, something like this:
template <typename T>
void A<Nullable<T::fn(const std::string& s)
{}

Of course it won't compile. Is there any solution for this?
Thanks
m.

Mar 20 '07 #1
3 1303
On Mar 20, 9:14 am, "mliptak" <Meht...@gmail.comwrote:
Hi, I need to specialize one operation in a template class, when the
parameter is a template itself.
Suppose this code:

#include <string>

template <typename T>
struct Nullable
{};

template <typename T>
class A
{
public:
void fn(const std::string& s)
{}

};

template<>
void A<int>::fn(const std::string& s)
{
// this is supposed to be A::fn() specialization for type int

}

int main()
{
A<inta;
a.fn(""); // ok, invokes the specialized fn()
A<charach;
ach.fn(""); // ok, invokes the generic fn()
A<Nullable<int an;
an.fn(""); // ok, invokes the generic fn()
return 0;

}

I need to specialize A::fn() for Nullable<>, something like this:
template <typename T>
void A<Nullable<T::fn(const std::string& s)
{}

Of course it won't compile. Is there any solution for this?
Thanks
m.
Remember your function 'fn' is not a template...Your class 'A is a
template.
So I would specialize class A for Nullable..

template <typename T, template<typename class Val >
class A<Val<T
{
public:
void fn(const std::string& s)
{}
};

Mar 20 '07 #2
On Mar 20, 9:14 am, "mliptak" <Meht...@gmail.comwrote:
Hi, I need to specialize one operation in a template class, when the
parameter is a template itself.
Suppose this code:

#include <string>

template <typename T>
struct Nullable
{};

template <typename T>
class A
{
public:
void fn(const std::string& s)
{}

};

template<>
void A<int>::fn(const std::string& s)
{
// this is supposed to be A::fn() specialization for type int

}

int main()
{
A<inta;
a.fn(""); // ok, invokes the specialized fn()
A<charach;
ach.fn(""); // ok, invokes the generic fn()
A<Nullable<int an;
an.fn(""); // ok, invokes the generic fn()
return 0;

}

I need to specialize A::fn() for Nullable<>, something like this:
template <typename T>
void A<Nullable<T::fn(const std::string& s)
{}

Of course it won't compile. Is there any solution for this?
Thanks
m.
Also, functions cannot be partically specialized.

Mar 20 '07 #3

am******@gmail.com wrote:
On Mar 20, 9:14 am, "mliptak" <Meht...@gmail.comwrote:
Hi, I need to specialize one operation in a template class, when the
parameter is a template itself.
Suppose this code:

#include <string>

template <typename T>
struct Nullable
{};

template <typename T>
class A
{
public:
void fn(const std::string& s)
{}

};

template<>
void A<int>::fn(const std::string& s)
{
// this is supposed to be A::fn() specialization for type int

}

int main()
{
A<inta;
a.fn(""); // ok, invokes the specialized fn()
A<charach;
ach.fn(""); // ok, invokes the generic fn()
A<Nullable<int an;
an.fn(""); // ok, invokes the generic fn()
return 0;

}

I need to specialize A::fn() for Nullable<>, something like this:
template <typename T>
void A<Nullable<T::fn(const std::string& s)
{}

Of course it won't compile. Is there any solution for this?
Thanks
m.

Remember your function 'fn' is not a template...Your class 'A is a
template.
So I would specialize class A for Nullable..

template <typename T, template<typename class Val >
class A<Val<T
{
public:
void fn(const std::string& s)
{}
};
Great, that works.. Thanks..
However, my original class A has more operations, than this one and I
only need to specialize open operation (fn()) - is there a way so I
don't need to duplicate all the other operations?
m.

Mar 21 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: SainTiss | last post by:
Hi, If you've got a template class with lots of methods, and then you've got a type which works with the template, except for one method... What you need to do there is specialize the...
8
by: Agent Mulder | last post by:
Hi group, I have a problem with partial template specialization. In the code below I have a template struct Music with one method, play(), and three kinds of music, Jazz, Funk and Bach. When I...
2
by: Xenos | last post by:
The new version of GCC is out and in its list of changes, it talks about the C++ Standard's requirements for using the typename and template keywords to disambiguate dependent names. I'm use to...
13
by: Imre | last post by:
Please take a look at the following code: template <typename T, typename Enable = void> class A { public: enum { value = 0 }; }; template <typename T>
1
by: Kai-Uwe Bux | last post by:
Hi folks, I would like to know which clause of the standard rules out this: template < typename eval > struct recursive_template { typedef typename eval::enum_type Enum;
9
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second...
6
by: Andre Kempe | last post by:
hej folks. i have a heap with fixed size and want to determine the depth of a element with given index at compile-time. therefore i wrote some templates. however, when i use template...
8
by: mattias.nissler | last post by:
Hi! Here is a problem I ran into at work. The following example doesn't compile on gcc-4.1: struct cons_end {}; template<typename U,typename Vstruct cons { U elem; V tail;
4
by: David Sanders | last post by:
Hi, I have a class with an integer template parameter, taking values 1, 2 or 3, and a function 'calc' in that class which performs calculations. Some calculations need only be performed if the...
8
by: flopbucket | last post by:
Hi, I want to provide a specialization of a class for any type T that is a std::map. template<typename T> class Foo { // ... };
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.