The following doesn’t compile, how can I do this? I think the example shows my intent, but I’ll try to add a blurb if people are confused.
template<typename T>
class A
{
private:
struct B
{
template<typename T2>
B& operator=( const A<T2>::B& right ){} // how can I make this work?
};
template<typename T2> friend class A;
template<typename T2> friend class A<T2>::B;
};
The fundamental idea of your assignment operator is flawed, as even with the addition of a
typename, it’s a non-deducible context. As such, the template parameter will never be deduced and the assignment operator will never work unless you explicitly specify a type likeB.operator=<some_type>(other_B).An easier version would be to just make it a normal function template, and SFINAE your way out.
Now all that’s left is the
is_a_Btype trait. You can make this easy on yourself with possible false positives:Just provide the
I_am_a_B_typetypedef in yourBclass.Live example on Ideone. Comment out the
b1 = 5;line and it compiles as seen here.And now for the
slightly morepervertedcomplicated way with no false-positives. 🙂For this one you need a
typedef A<T> parent;insideB. It’s staged in two parts:parenttypedef exists, and if it doesAclass template.Sadly, the logical operators (
&&,||,?:) don’t short-circuit in template code like I hoped, so I had to write thoseand_vtemplates + a lazy tester that only gets evaluated if aparenttypedef exists.Live example on Ideone. Again, comment out the
b1 = 5;line to make it compile as seen here.