Re: null assignment in a template
- From: "Alex Blekhman" <xfkt@xxxxxxxxx>
- Date: Fri, 25 May 2007 13:20:00 +0300
"Steve Wolf" wrote:
I find it hard to believe that there isn't a clever way to solve this using type traits, partial specialization, and/or policy templates.
Thinking again about your question I came up with following code:
-----------------------------
template <typename T>
struct X
{
...
T* operator =(const T* p)
{
if(p == NULL) { ... }
m_p = p;
return m_p;
}
T* m_p;
};
struct Y {};
X<Y> x;
x = &y; // OK
x = NULL; // OK
x = 42; // Error!
-----------------------------
Now, in the above snippet you can see that pointer type assignment (including NULL, as a spcial case) goes through `operator='. While any other integer value assignment fails to compile. Isn't it what you want?
Alex
P.S. I'm using VC++ 2005 SP1.
.
- References:
- null assignment in a template
- From: Steve Wolf
- Re: null assignment in a template
- From: Alex Blekhman
- Re: null assignment in a template
- From: Steve Wolf
- null assignment in a template
- Prev by Date: Re: null assignment in a template
- Next by Date: Re: null assignment in a template
- Previous by thread: Re: null assignment in a template
- Next by thread: Re: null assignment in a template
- Index(es):