Template specialization with the parameter being a template

M

mliptak

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<int> a;
a.fn(""); // ok, invokes the specialized fn()
A<char> ach;
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.
 
A

amparikh

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<int> a;
a.fn(""); // ok, invokes the specialized fn()
A<char> ach;
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)
{}
};
 
A

amparikh

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<int> a;
a.fn(""); // ok, invokes the specialized fn()
A<char> ach;
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.
 
M

mliptak

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top