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. 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)
{}
};
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. 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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>
|
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;
|
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...
|
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...
|
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;
|
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...
|
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
{
// ...
};
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
|
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...
| |