C++ templates that accept only certain types

前端 未结 14 2655
一整个雨季
一整个雨季 2020-11-22 11:37

In Java you can define generic class that accept only types that extends class of your choice, eg:

public class ObservableList {
  ...
         


        
14条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 11:57

    class Base
    {
        struct FooSecurity{};
    };
    
    template
    class Foo
    {
        typename Type::FooSecurity If_You_Are_Reading_This_You_Tried_To_Create_An_Instance_Of_Foo_For_An_Invalid_Type;
    };
    

    Make sure derived classes inherit the FooSecurity structure and the compiler will get upset in all the right places.

提交回复
热议问题