I have the following in a 3rd party core header, which i would prefer not to touch:
template<typename Iterator_T>
struct Tree
{
template<typename Node_T>
struct TypedNode;
struct AbstractNode
{
AbstractNode(Iterator_T t)
{
...
}
template<typename Rule_T>
TypedNode<Rule_T>* NewChild()
{
TypedNode<Rule_T>* ret = new TypedNode<Rule_T>(this);
AddChild(ret);
return ret;
}
template<typename T>
TypedNode<T>* GetFirstTypedChild();
};
template<typename Node_T>
struct TypedNode : AbstractNode
{
...
};
template<typename Rule_T, typename ParserState_T>
void CreateNode(ParserState_T& p)
{
current = current->template NewChild<Rule_T>();
}
};
template<typename Node_T>
struct TreeBuilder
{
Tree<Iterator_T> tree;
template<typename Rule_T>
void CreateNode()
{
tree.CreateNode<Rule_T>(*this);
}
};
and would like to add a subclass of TypedNode to the tree with additional methods and members. Is there a clean way to do this without refactoring the original code? I think the crux of the issue is:
TypedNode* ret = new TypedNode(this);
This looks like it could use a Factory, perhaps by specializing the TypedNode template instantiation and including the header before the base headers. Something like:
template<>
struct Tree<const char*>::TypedNode<SpecificRuleType>
{
...
};
But it fails complaining “too few template-parameter-lists”. Probably something trivial but my <template-fu> is a bit rusty :>(. Any ideas would be appreciated!
Both
TreeandTypedNodeare templates, your specialization should be: