C++11 template: How to ensure that the type inherits a class?

Yves

In Java generic, when I want to ensure that the type of some generic class must inherit a class, I can code as below:

public class MyHandler<T extends Serializable> {}

This means that T must extend/implement the class/interface Serializable, otherwise, the compiler will generate an error.

How to get the same thing in C++11? I mean, if I code C++11 as below:

template<typename T>
class MyHandler {}

In this piece of code, T can be any class. But, if I want to tell the class users that T must inherit the class boost::archive::text_oarchive (just like <T extends Serializable> in Java), what can I do?

songyuanyao

You can use std::is_base_of for checking.

template<typename T>
class MyHandler {
    static_assert(std::is_base_of<boost::archive::text_oarchive, T>::value, "T must inherit boost::archive::text_oarchive");
};

Since C++20 we can use constraint, e.g.

template<typename T> requires std::is_base_of_v<boost::archive::text_oarchive, T>
class MyHandler {};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I ensure type hints still work for my class that inherits UserList from collections?

How to Determine Underlying Template Type of Template Parameter That Inherits From a Class Template?

How to elegantly check if the template type is derived from the specific base class in C++11?

C++11 Translating a variadic template to deduce a class type

How to force/ensure class attributes are a specific type?

How to force/ensure class attributes are a specific type?

How to Ensure proper initialization of Non Static Data Members within a Class Template in C++

Using Generics in Base Classes in C#: How to ensure methods in the base class return the derived class's type?

How can I deduce a template parameter type in C++11?

c++11 how to convert legacy class to template

How to redefine the template class constructor via a macro in C++11?

C# Error CS0738 when class inherits from interface and with type that inherits from interfce type method

Check C++ class publicly inherits from template class with anonymous parameter

How to ensure in C# that the type of an object is equal to this?

How to disable c# Attribute that inherits from parent class?

How do I get a type of a class at runtime with a string that inherits from an abstract class and use the retrieved type as a parameter

Exporting a class from a dll that inherits from a template

C++ "assuming" template type in a template class

How to create user defined type template class object C++

C++ How to override class field with a different type (without template)?

C++ How to cache a variable of template type T in a class?

How to Create Template Class object of auto type in C++?

Ensure template parameter is an enum class

Deprecate templated class name with template alias (type alias, using, C++11)?

Can I create a template (non-type) param class that can take any enum? C++11

Template static class with string list as template parameter, how to make on c++ 11

How to realize template class type in template function?

How to condition a class template and primitive type template

How to tell if template type is an instance of a template class?