I am having trouble figuring out how to prevent recursive attempts at template substitution and explicitly force a sub-class template instantiation
The following code:
#include <boost/ptr_container/ptr_vector.hpp>
template <typename TemplateParameter>
struct ParentClass
{
struct SubClass
{
boost::ptr_vector<SubClass> nodes;
}; // this by itself works fine
// but with combination with this
void someMethod(boost::ptr_vector<SubClass>& otherNodes){}
};
int main (int argc, char** argv)
{
ParentClass<int> p;
return 0;
}
built with boost 1.68 and gcc 7.3.1 gives the following error:
test.cpp: In instantiation of ‘struct ParentClass<int>::SubClass’:
/usr/include/boost/ptr_container/nullable.hpp:55:13: recursively required by substitution of ‘template<class T> boost::type_traits::yes_type boost::ptr_container_detail::is_nullable(const boost::nullable<T>*) [with T = <missing>]’
/usr/include/boost/ptr_container/nullable.hpp:55:13: required from ‘const bool boost::is_nullable<ParentClass<int>::SubClass>::value’
/usr/include/boost/mpl/if.hpp:63:11: required from ‘struct boost::mpl::if_<boost::is_nullable<ParentClass<int>::SubClass>, ParentClass<int>::SubClass, boost::mpl::identity<ParentClass<int>::SubClass> >’
/usr/include/boost/mpl/eval_if.hpp:37:41: required from ‘struct boost::mpl::eval_if<boost::is_nullable<ParentClass<int>::SubClass>, ParentClass<int>::SubClass, boost::mpl::identity<ParentClass<int>::SubClass> >’
/usr/include/boost/ptr_container/nullable.hpp:69:13: required from ‘struct boost::remove_nullable<ParentClass<int>::SubClass>’
/usr/include/boost/ptr_container/nullable.hpp:80:55: required from ‘struct boost::ptr_container_detail::void_ptr<ParentClass<int>::SubClass>’
test.cpp:11:10: required from ‘struct ParentClass<int>’
test.cpp:16:22: required from here
test.cpp:8:37: error: invalid use of incomplete type ‘struct boost::ptr_container_detail::void_ptr<ParentClass<int>::SubClass>’
boost::ptr_vector<SubClass> nodes;
^~~~~
In file included from /usr/include/boost/ptr_container/detail/reversible_ptr_container.hpp:25,
from /usr/include/boost/ptr_container/ptr_sequence_adapter.hpp:20,
from /usr/include/boost/ptr_container/ptr_vector.hpp:20,
from test.cpp:1:
/usr/include/boost/ptr_container/nullable.hpp:75:16: note: declaration of ‘struct boost::ptr_container_detail::void_ptr<ParentClass<int>::SubClass>’
struct void_ptr
^~~~~~~~
My understanding of the issue is that it tries to resolve the ParentClass
and consequently tries to resolve its method - someMehtod(...)
but in order to do it must resolve the SubClass
which is defined. A workaround is to force an explicit instantiation by instantiating SubClass
as a member of ParentClass
:
struct SubClass
{
boost::ptr_vector<SubClass> nodes;
} member;
Is there another, less hacky way? :)
The issue was not present on older version of boost, where boost::ptr_vector
was implemented with void*
instead of void_ptr
template class.