So in my C++11 project, I'm trying to alias a specific traits template specialisation based on a class template parameter. This is intended so that I can inherit from the template, using CRTP, and access the members of the traits template without needing to refer to the type used to specialise it.
hal.h:
template <class Implementation>
struct HAL_Traits
{};
template <class Implementation>
class HAL
{
public:
using ConfigurationParameters = HAL_Traits<Implementation>;
void DoStuff()
{
static_cast<Implementation*>(this)->DoStuff();
}
}
hal_implementation.h:
template<>
HAL_Traits<Implementation>
{
//test data member
static constexpr auto Array1 = MakeConstArray<1,2,3>();
}
class Implementation: public HAL<Implementation>
{
public:
void DoStuff();
};
hal_implementation.c:
void Implementation::DoStuff()
{
//ConfigurationParameters should be the inherited alias from HAL, ie
// resolve to HAL_Traits<Implementation>
DoSomethingWith(ConfigurationParameters::Array1);
}
However if I call ImplementationInstance.DoStuff() I get the following compiler error:
undefined reference to `HAL_Traits<Implementation>::Array1'
Given the error it looks like the compiler is correctly resolving the alias, but then unable to find the class member. From what I've been able to see so far this is potentially an issue with dependent names, especially given the error only occurs if I try to call DoSomething, which I believe instantiates the template, but most of the other questions on similar topics that I could find here on Stack were not referring to C++11.
Is it possible that the dependent/non-dependent names issue is causing the compiler to treat HAL_Traits<Implementation> as a distinct type rather than a specialization of the template HAL_Traits?
I did consider that this is an issue with the order of things being declared. I really don't want to define the specialised HAL_Traits<Implementation> in hal.h between HAL_Traits and HAL, because I'm trying to keep the concrete implementation details for each vendor separate from the universal interface that HAL presents. Including hal_implementation.h before hal.h didn't seem to make any difference to the error I received.
I've worked with templates before, but this is the first time I've used CRTP or traits. Is there some way to achieve what I'm aiming for here?
Aucun commentaire:
Enregistrer un commentaire