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

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -