c++ - Template assignment operator in template class copying inner class issue -
i have template class containing inner class. on operator=() overload want copy instance of inner class error:
no known conversion 'const foo<double>::bar' 'const foo<int>::bar'
the code looks (you can find example here):
template<typename t> class foo { public: template<typename> friend class foo; class bar { std::string inner_str; public: bar const& operator=(bar const& r) { inner_str = r.inner_str; return *this; } }; template<typename f> foo const& operator=(foo<f> const& r) { value = static_cast<t>(r.value); str_container = r.str_container; return *this; } private: bar str_container; t value; };
moving bar
outside foo
works fine, inner class should part of foo
.
how solve issue? thought making bar
friend foo friend specializations of foo. don't know how introduce them.
sidenote: please feel free change title since don't know how title specific problem.
foo<double>::bar
unrelated type const foo<int>::bar
, cannot convert between them implicitly.
since bar
appears not depend on template argument, seem sensible solution:
moving bar outside foo works fine
problem solved :)
but inner class should part of
foo
.
considering how use it, argue no, should not part of foo
.
alternatively, make assignment operator of bar
template did assignment operator of foo
:
template<typename b> bar const& operator=(const b& r) { inner_str = r.inner_str; return *this; } template<typename u> friend class foo<u>::bar; // bar must friends other bars
if definition of bar
depend on template argument of foo
, use approach. otherwise, wouldn't make sense define identical, separate inner class inside every foo
.
Comments
Post a Comment