1

Topic: Restriction on parameters of templates

Here I want to write, for example, any such sample function:

template <class Node>
void addNode (std:: shared_ptr <CompositeNode> AND currentNode, const std:: string AND line) {
std:: shared_ptr <Node> node = std:: make_shared <Node> (currentNode);
node-> readAttributes (line);
currentNode-> addChild (node);
}

Function accepts  a tree and should create and add to it the new child. Thus, there are some types  and which needs to be created define causing code and should transfer this  in function. For this purpose I use a template.
Actually a question, whether it is possible to set somehow restriction on parent class of parameter of a template? For example, in  it would be possible to write something like <Node extends TreeNode> (truth, in  to create  it should through one place a reflection, but already another story altogether, ), and in With ++, I so understand, so it is impossible also all jambs in function contents will be found out, only when someone tries to cause it?

2

Re: Restriction on parameters of templates

Rusty nail;
std:: enable_if together with std:: is_base_of

3

Re: Restriction on parameters of templates

Should be somehow so

template <class Node>
std::enable_if_t<std::is_base_of<TreeNode, Node>:: value, void>
addNode (std:: shared_ptr <CompositeNode> AND currentNode, const std:: string AND line) {
std:: shared_ptr <Node> node = std:: make_shared <Node> (currentNode);
node-> readAttributes (line);
currentNode-> addChild (node);
}

4

Re: Restriction on parameters of templates

a nail wrote:

whether it is possible to set somehow restriction on parent class of parameter of a template?

It is possible to throw out at all a template and... Everything, restriction will be entered automatically at the job in
Quality of parameter of this parent class.

5

Re: Restriction on parameters of templates

Dimitry Sibiryakov wrote:

It is possible to throw out at all a template and... Everything, restriction will be entered automatically at the job in
Quality of parameter of this parent class.

As I understand, at it the parent class does not figure in function arguments.

6

Re: Restriction on parameters of templates

NekZ wrote:

at it the parent class does not figure in function arguments.

And in vain. The correct inheritance and polymorphism - is much more convenient . On the extreme
To measure - is much more readable.

7

Re: Restriction on parameters of templates

Dimitry Sibiryakov wrote:

And in vain. The correct inheritance and polymorphism - is much more convenient . On the extreme
To measure - is much more readable.

With it it is impossible to argue. But occasionally there are such situations, when without the such
Sample  not to manage.

8

Re: Restriction on parameters of templates

NekZ wrote:

As I understand, at it the parent class does not figure in function arguments.

Aha, so.

NekZ wrote:

Should be somehow so

Thanks that is necessary!

9

Re: Restriction on parameters of templates

a nail wrote:

it is passed...
Aha, so.
it is passed...
Thanks that is necessary!

It is necessary to you explicitly  function with specified class Node.
addNode <NodeType1> (currentNode, line);
addNode <NodeType2> (currentNode, line);
And so on.