Template inheritance in C++ (porting Java code)

I would like to port a Java programm to C++ which is using generics in one class.

public class Graph<T extends Node> {
  ...
}

I already stumpled across this problem a couple years ago and couldn't solve it because C++ does not support this in those days. Is this now possible in C++11 or is there any method to force the compiler to check if the generic type has the baseclass Node inherited?

template <typename T : public Node>
class Graph {
  ...
};

Thanks for your help!

Answers


is there any method to force the compiler to check if the generic type has the baseclass Node inherited?

You are looking for std::is_base_of. Now, to make it a compile time check, you could use it with static_assert.

In my opinion, it is a matter of judgment whether to enforce that T must derive from Node. If T implements all the necessary methods so that the Graph class compiles, I see no reason to enforce that T must be derived from Node.


Need Your Help

alternative to mysql_real_escape_string (access_denied)

php mysql

Im using sphinxql currently and using mysql_real_escape_string on my $query but since there is no database connection...I get an access denied. I was looking for alternatives :

Node.js with scheduler and multiple instances in Cloud Foundry

node.js scheduler cloudfoundry

I would like to have a Node.js app running in multiple instances in Cloud Foundry.