Reputation: 2501
With more detail added
I'm looking to inject in a type and use this as the template type, in addition to this I would like to implicitly pass in a subtype to be used.
Implementation:
MyClass<MyTestClass> myClass;
const auto& testDataReturn = myClass.m_typeObject.m_field1;
// looking to be able to call myClass.m_subTypeObject here which is implicitly typed from the parent type (see template def below)
Template:
template<class T>
class MyClass
{
public:
T m_typeObject;
typename T::SubType m_subTypeObject; // **does not like this**
};
typedef MyType SubType;
Class definitions:
class SubTypeImpl
{
public:
std::string m_field1 = "test1Sub";
std::string m_field2 = "test2Sub";
};
class MyTestClass
{
public:
std::string m_field1 = "test1";
SubTypeImpl SubType;
};
Upvotes: 1
Views: 423
Reputation: 685
I may not fully understand the question, but it seems if you change
SubTypeImpl SubType;
to
typedef SubTypeImpl SubType;
you can achieve what you want.
Upvotes: 1
Reputation: 275340
T::SubType
is not a type, it is a member field.
template<class T>
using SubType = decltype(std::declval<T&>().SubType);
that gets the type of the SubType
member of T
, assuming it is public.
template<class T>
class MyClass {
public:
T m_typeObject;
SubType<T> m_subTypeObject;
};
you could do the decltype
inline in the MyClass
definition, but that is messy.
Upvotes: 2