c++ - 充分利用 static_assert 和 std::is_invocable

标签 c++ c++17 sfinae static-assert

我有一个包含多个函数对象的库,这些函数对象可能只接受几种类型,具体取决于 std::is_integral .我要std::is_invocable在条件失败时返回 false,但当用户尝试调用函数对象的实例时,我还想要一个不错的 static_assert 错误消息。这是我目前拥有的函数对象的一个​​简化示例:

struct function
{
    template<typename Iterator>
    auto operator()(Iterator first, Iterator last) const
        -> std::enable_if_t<std::is_integral_v<
            typename std::iterator_traits<Iterator>::value_type
        >>
    { /* something */ }
};

通过这样的实现,std::is_invocablestd::false_type 当不满足 SFINAE 条件时,如预期的那样,但用户在他们遇到难看的 SFINAE 错误消息时尝试使用不满足 SFINAE 条件的参数调用函数对象。

为了获得更好的错误消息,我尝试了以下解决方案:

struct function
{
    template<typename Iterator>
    auto operator()(Iterator first, Iterator last) const
        -> void
    {
        static_assert(std::is_integral_v<typename std::iterator_traits<Iterator>::value_type>,
                      "function can only be called with a collection of integers");

        /* something */
    }
};

通过此实现,当不满足原始 SFINAE 条件时,用户会收到友好的错误消息,但 std::is_invocablestd::true_type 当被问及是否 function 实例可以处理不满足 std::is_integral 的类型。

我尝试了一些涉及 decltype(auto)if constexpr 和其他机制的技巧和变体,但无法获得错误消息很好且 的类code>std::is_invocable 在询问是否可以使用不正确的类型调用 function 时对应于预期的 std::false_type

我在这里错过了什么?有没有办法同时获得正确的 std::is_invocable 用户友好的错误消息?

最佳答案

这是一个糟糕的方法。添加重载:

template <typename Iterator>
auto operator()(Iterator first, Iterator last) const
    -> std::enable_if_t<std::is_integral_v<
        typename std::iterator_traits<Iterator>::value_type
    >>;

template <typename Iterator, class... Args>
void operator()(Iterator, Iterator, Args&&... ) const = delete; // must be integral

这满足 is_invocable<>条件,因为受限函数模板更专业和更受欢迎 - 所以如果满足条件,则函数是可调用的,否则将被删除。

这在错误情况下做得更好一些,因为如果你尝试调用它,你会得到:

foo.cxx: In function ‘int main()’:
foo.cxx:31:13: error: use of deleted function ‘void function::operator()(Iterator, Iterator, Args&& ...) const [with Iterator = std::__cxx11::basic_string<char>*; Args = {}]’
     f(&i, &i);
             ^
foo.cxx:19:10: note: declared here
     void operator()(Iterator, Iterator, Args&&... ) const = delete; // must be integral
          ^~~~~~~~

注释显示在错误消息中,有点像静态断言消息?


这实际上是Concepts的动机之一。用requires而不是 enable_if ,我们得到:

foo.cxx: In function ‘int main()’:
foo.cxx:26:13: error: no match for call to ‘(function) (std::__cxx11::string*, std::__cxx11::string*)’
     f(&i, &i);
             ^
foo.cxx:11:10: note: candidate: void function::operator()(Iterator, Iterator) const requires  is_integral_v<typename std::iterator_traits<_Iter>::value_type> [with Iterator = std::__cxx11::basic_string<char>*]
     void operator()(Iterator first, Iterator last) const
          ^~~~~~~~
foo.cxx:11:10: note:   constraints not satisfied
foo.cxx:11:10: note: ‘is_integral_v<typename std::iterator_traits<_Iter>::value_type>’ evaluated to false

那是……我想好一点。

关于c++ - 充分利用 static_assert 和 std::is_invocable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44978803/

相关文章:

c++ - switch 语句中预期的常量表达式惨败

c++ - 使用 enable_if 的多个重载问题

c++ - Qt Quick2窗口没有aero不能透明

c++ - constexpr 算法 all_of 编译器错误

c++ - 自定义容器中括号运算符的常量

c++ - copy_file 何时实际返回 false?

c++ - 函数模板参数推导模板参数vs默认模板参数vs返回类型

c++ - 标准是否要求 std::tuple_size 对 SFINAE 友好?

c++ - 在运行时确定 std::map/std::set 的内存使用

c++ - gcc 何时编译未使用的模板代码?