views:

254

answers:

3

-edit- i am trying to close the question. i solved the problem with boost::is_base_and_derived

In my class i want to do two things. 1) Copy int, floats and other normal values 2) Copy structs that supply a special copy function (template T copyAs(); } the struct MUST NOT return int's unless i explicitly say ints. I do not want the programmer mistaking the mistake by doing int a = thatClass;

-edit- someone mention classes dont return anything, i mean using the operator Type() overload.

How do i create my copy operator in such a way i can copy both 1) ints, floats etc and the the struct restricted in the way i mention in 2). i tried doing

template <class T2>
T operator = (const T2& v);

which would cover my ints, floats etc. But how would it differentiate from structs? so i wrote

T operator = (const SomeGenericBase& v);

The idea was the GenericBase would be unsed instead then i can do v.Whatever. But that backfires bc the functions i want wouldnt exist, unless i use virtual, but virtual templates dont exist. Also i would hate to use virtual

I think the solution is to get rid of ints and have it convert to something that can do .as(). So i wrote something up but now i have the same problem, how does that differentiate ints and structs that have the .as() function template?

+3  A: 

Sorry AcidZombie, but I find your question almost unparseable.

What do you mean you want your class to copy ints, floats, and other normal values? "normal" (ie: inherent) types copy with the already supplied assignment operator (=). But I'm confident you already know that.

You also write "the struct MUST not return int's...". struct's don't return anything. Functions return values. structs are storage for keeping values.

Please consider re-wording your question significantly to clearly illustrate what works, what doesn't work, what you've tried, and what your question is.

abelenky
you have made a tiny, insignificant edit to the OP, which did not address any of the problems I raised. Down-voting this question.
abelenky
A: 

I assume your class is something like:

template <class T>
class Copier
{
public:
    template<class T2>
    T& operator=(const T2&) {}
};

You can specialize the operator= like this:

template <class T>
class Copier
{
public:
    template<class T2>
    T& operator=(const T2&) {}
    template<int>
    T& operator=(const int&) {}
};

I don't know, exactly, the syntax for out-of-class specialization, and this seems like a poor design anyway. Consider redesigning this class.

greyfade
A: 

use boost::is_base_and_derived

acidzombie24