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

Template specialization with the parameter being a template

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.