Q:

How to handle member variables in template stuff

I'm using boost fusion library to literate through all members in a struct. Identity is a member of Namespace::SomeClass. Through some recursive operations, v will eventually be a Namespace::SomeClass object. After entering the if statement, I could like to be able to handle the member objects of SomeClass.
template<typename T>
void Function(T const& v)
{
    typedef boost::mpl::range_c<unsigned, 0, boost::fusion::result_of::size<T>::value > T_members;
    boost::fusion::for_each(T_members(), FunctionStruct<T>(v));

    if (std::is_same<T, Namespace::SomeClass>::value)
    {
        v.Identity= "0";
    }
}

When I compile this code, it gives me an C2039 error that says

'Identity': is not a member of 'Namespace::OtherClass'.

Since I'm only handling member identity when v is a SubClass, Identity doesn't have to be a member of OtherClass. How should I write my code to not have this compilation error?
Thanks!

A:

Regardless of the fact that the check
if (std::is_same<T, Namespace::SomeClass>::value)

is a compile time constant, the compiler still has to compile all the code in the function. If Identity is only a member of T if that check is true, then it'll clearly fail to compile if that check fails. You'll have to move it into a different function. The simplest approach for that is tag dispatching:
zero_out(v, std::is_same<T, Namespace::SomeClass>{});

with:
template <class T>
void zero_out(T const& v, std::true_type) {
    v.Identity = 0;
}

template <class T>
void zero_out(T const& v, std::false_type) {
    // nothing ?
}

Or in this case, since we're checking specific types, don't even need tag dispatching, can just provide a non-template overload:
zero_out(v);

template <class T>
void zero_out(T const& ) { }

void zero_out(Namespace::SomeClass const& v) {
    v.Identity = 0;
}

Side-note, you're assigning a member on a const reference?

