I’m new to CRTP and heavily templated code so I’m not sure if this is possible…
I have a template class as follows:
template<typename ...T>
class base_type_components
{
public:
template<std::size_t i>
std::tuple_element_t<i, std::tuple<T...>>
component()
{
return std::get<i>(_components);
}
constexpr std::size_t
num_components() const
{
return sizeof...(T);
}
private:
std::tuple<T...> _components;
}
This parent class takes a list of types and generates a tuple for ‘component storage’.
I can access these components by calling x.component<0>()
, &c. However my child class is also a template, so I actually use this->template component<0>()
internally.
My child class is defined as so:
template<std::size_t L = 199>
class type_derived_st :
public base_type_components<type_derived_st<L>, std::string>
{
public:
std::string
value() const
{
return this->template component<0>();
}
...
}
This class is also a template. The template args are used elsewhere in other code unrelated to the problem.
The problem lies in the return statement for type_derived_st<L>::value
.
When I instantiate the class as type_derived_st<5>
and try to run the value
function, I get the following compile-time error:
error: cannot convert
'const type_derived_st<5>*'
to'base_type_components<type_derived_st<5>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >*'
I don’t understand what is preventing conversion here?
If I do the following:
type_derived_st<5> x;
std::string s = x.component<0>();
Then it works perfectly. It’s only when I try to call the function internally to the template derived class that this error occurs.