Re: null assignment in a template



"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.


.