C++ ranges-v3 concepts::valid_expr checks, with trailing ", 42"

77 views Asked by At

Continuing my reading of ranges-v3 library, I realized that all of checks about valid expressions for template types has a tailing ", 42" expression, and I wonder what is the purpose of that. For example:

namespace concepts {
    constexpr struct valid_expr
    {
       template<typename... T>
       void operator()(T&&...) const;
    };
}

struct ExplicitlyConvertibleTo
{
   template<typename From, typename To>
   auto requires_(From (&from)()) -> decltype(
       concepts::valid_expr(
           ((void) static_cast<To>(from()), 42)
   ));
};

I understand some of the points of that implementation, like the inner parenthesis to force the use of the comma operator, the void-casting to avoid some overloads of the comma operator, etc, but why not just simply writting something like?

concepts::valid_expr(static_cast<To>(from()));
1

There are 1 answers

0
Jeff Garrett On

A small correction: In range-v3, valid_expr is an object not a type:

constexpr struct valid_expr_t { /*...*/ } valid_expr;

Now, consider what happens if you use concepts::valid_expr(static_cast<To>(from())) as suggested and To is void. It is allowable to static_cast to void, but it is not allowable to call a function with an argument which is a void expression.