How to properly use std::enable_if on a constructor

前端 未结 1 1245
长发绾君心
长发绾君心 2021-01-28 08:36

This question combines several pieces of code and is a bit complicated, but I tried slimming it down as much as possible.

I am trying to use std::enable_if

1条回答
  •  花落未央
    2021-01-28 09:24

    Dependent names

    typename function_traits::template arg<0>::type
                                      ^^^^^^^^
    

    See this post for more information on dependent names and when template or typename is needed.

    enable_if

    typename = std::enable_if_t
    

    should instead be

    std::enable_if_t* = nullptr
    

    as @Jarod42 mentioned. This is because the constructors would otherwise be identical and unable to be overloaded. That their default values differ doesn't change this fact. See this for more information.

    Putting it together is

    template::template arg<0>::type, a_type>>* = nullptr>
    A(const Lambda&);
    

    Live

    Side note

    function_traits won't work with either overloaded or templated operator(), it can instead be replaced

    template
    using return_type = decltype(std::declval()(std::declval()...));
    
    template
    using mfp = decltype(static_cast(T::*)(Args...) const>(&T::operator()));
    
    template = nullptr>
    A(const Lambda&);
    

    To check if the callable can be called with the exact arguments without conversions.

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