繁体   English   中英

用 C++17 fold-expression 测试所有元素是否相等

[英]Test if all elements are equal with C++17 fold-expression

我有一个采用可变参数包的函数,一开始我想检查所有元素是否相等。 我可以以某种方式使用新的 C++17 折叠表达式将其简洁地写成一行吗? 我在想

template<typename... Args>
void func (Args... args)
{
    ASSERT ((args == ...));

    // more code here...
}

但这不起作用,因为它编译为首先正确比较最后两个参数的代码,然后将倒数第三个参数与第一次比较的结果进行比较,这是一个布尔值。 这种类型的折叠表达式可能有什么用例(类似于args < ... )? 我是否有机会避免编写专用的递归模板来执行此操作?

不幸的是,这不起作用的原因是因为布尔运算符不像在其他语言中那样在 C++ 中链接。 所以表达式:

a == (b == c)

(你的折叠表达式会扩展到什么)将atruefalse进行比较,与bc实际是什么无关。 我希望operator<=>会添加链接,但显然该部分已被删除。

解决方法是您必须打破比较:

(a == b) && (b == c)

当然,这并不适合折叠,但您可以将所有内容与第一个元素进行比较:

(a == b) && (a == c)

也就是说:

((a0 == args) && ... )

那时,我们只需要能够拉出第一个元素。 没问题,这显然是 lambdas 的用途:

template <class... Args>
constexpr bool all_equal(Args const&... args) {
    if constexpr (sizeof...(Args) == 0) {
        return true;
    } else {
        return [](auto const& a0, auto const&... rest){
            return ((a0 == rest) && ...);
        }(args...);
    }
}

正如 Piotr Skotnicki 所建议的,一个简单的解决方案是将第一个参数与以下参数分开,并使用&&作为折叠运算符进行检查

例如,如果所有参数都相等,则以下函数返回true

template <typename A0, typename ... Args>
bool foo (A0 const & a0, Args const & ... args)
 { return ( (args == a0) && ... && true ); } 

不幸的是,这不适用于空的参数列表

std::cout << foo(1, 1, 1, 1) << std::endl; // print 1
std::cout << foo(1, 1, 2, 1) << std::endl; // print 0
std::cout << foo() << std::endl;           // compilation error

但您可以添加特殊的空参数foo()

bool foo ()
 { return true; }

如果由于某种原因,您无法将args拆分为a0和以下args

嗯...你显然可以使用前面的foo()函数(带有特殊的空版本)

template<typename... Args>
void func (Args... args)
{
    ASSERT (foo(args));

    // more code here...
}

或者您可以使用带有逗号运算符和赋值的 C++17 折叠表达式,如下面的bar()

template <typename ... Args>
bool bar (Args const & ... args)
 {
   auto a0 = ( (0, ..., args) );
   return ( (args == a0) && ... && true ); 
 }

观察a0赋值中的初始零,这允许使用此解决方案也带有空参数列表。

不幸的是,从前面的auto a0赋值中,我收到了很多我不知道如何避免的警告(“表达式结果未使用”,来自 clang++,以及“逗号运算符的左操作数无效”,来自 g++)。

以下是一个完整的工作示例

#include <iostream>

template <typename A0, typename ... Args>
bool foo (A0 const & a0, Args const & ... args)
 { return ( (args == a0) && ... && true ); }

bool foo ()
 { return true; }

template <typename ... Args>
bool bar (Args const & ... args)
 {
   auto a0 = ( (0, ..., args) );
   return ( (args == a0) && ... && true ); 
 }

int main ()
 {
   std::cout << foo(1, 1, 1, 1) << std::endl; // print 1
   std::cout << foo(1, 1, 2, 1) << std::endl; // print 0
   std::cout << foo() << std::endl;           // print 1 (compilation error
                                              //          witout no argument
                                              //          version)

   std::cout << bar(1, 1, 1, 1) << std::endl; // print 1
   std::cout << bar(1, 1, 2, 1) << std::endl; // print 0
   std::cout << bar() << std::endl;           // print 1 (no special version)
 }

-- 编辑 --

正如 dfri 所指出的(谢谢!),for 和 empty args... pack,以下折叠表达式的值

( (args == a0) && ... )

( (args == a0) || ... )

分别是truefalse

所以foo()bar()返回指令可以无差别的写成

 return ( (args == a0) && ... && true );

 return ( (args == a0) && ... );

sizeof...(args) == 0U情况下也是如此。

但我倾向于忘记这类细节,而更喜欢明确(使用最后的&& true )空值。

这是我在gcl库中的做法:

template <auto ... values>
constexpr static auto equal_v = []() consteval {
    static_assert(sizeof...(values) > 0, "gcl::mp::value_traits::equal_v : no arguments");
    constexpr auto first_value = std::get<0>(std::tuple{values...});
    static_assert(
            (std::equality_comparable_with<decltype(values), decltype(first_value)> && ...),
            "gcl::mp::value_traits::equal_v : cannot compare values");
    return ((values == first_value) && ...); 
}();

或按概念要求替换static_assert

template <typename ... Ts>
concept are_equality_comparable = requires(Ts ... values)
{
    { 
        std::conditional_t<(std::equality_comparable_with<decltype(std::get<0>(std::tuple{values...})), decltype(values)> && ...), std::true_type, std::false_type>{}
    } -> std::same_as<std::true_type>;
};

template <auto ... values>
    requires(are_equality_comparable<decltype(values)...>)
constexpr static auto equal_v = []() consteval {
    static_assert(sizeof...(values) > 0, "gcl::mp::value_traits::equal_v : no arguments");
    constexpr auto first_value = std::get<0>(std::tuple{values...});
   
    return ((values == first_value) && ...); 
}();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM