How does std::enabled_if work when enabling via a parameter

前端 未结 1 1099
攒了一身酷
攒了一身酷 2021-01-25 05:18

I\'m trying to understand how enable_if works and I understand almost everything except scenario #3 from

https://en.cppreference.com/w/cpp/types/enable_if



        
相关标签:
1条回答
  • 2021-01-25 06:04

    why in enable_if is only condition without indicating second template parameter ?

    Because the default void is just fine.

    What type is "type*" then ? void* ? if so, why ?

    Yes, ::type will be of type void if std::is_trivially_destructible<T>::value == true, this will result in ::type* -> void*.

    Why is it pointer ?

    So we can easily give it a default value of 0.


    All we're using std::enable_if for is to check for certain attributes (in this case checking if T is trivially destructible), if these result in false then we use it to create ill-formed code and thus eliminate this function from overload resolution.

    If std::is_trivially_destructible<T>::value == false then ::type will not exist and thus the code will be ill-formed. In SFINAE this is handy since this overload will then not be considered for resolution.

    0 讨论(0)
提交回复
热议问题