How to detect existence of a class using SFINAE?

后端 未结 4 1504
花落未央
花落未央 2020-11-27 17:02

Is it possible to detect if a class exists in C++ using SFINAE? If possible then how?

Suppose we have a class that is provided only by some versions of a library. I\

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 17:24

    Still did not find a satisfying answer in this post...

    Mike Kinghan started the answer right and told a smart thing:

    So the problem is to determine whether T is a defined class type.

    But

    sizeof(T) is no help here

    is not correct...

    Here is how you can do it with sizeof(T):

    template 
    struct is_defined
    {
        static constexpr bool value = false;
    };
    
    template 
    struct is_defined 0)>>
    {
        static constexpr bool value = true;
    };
    

提交回复
热议问题