简体   繁体   中英

The syntax of a concept in C++20

How can we convert the requirements In this question link to a concept

I have attempted the following:

template< typename U, typename Tin, typename Tout>
concept MyConditions =
    (
         U::value_type
        &&  Tin::value_type
        &&  Tout::value_type
        && std::is_floating_point_v<typename Tin::value_type>
        && std::is_integral_v<typename U::value_type>
        && std::is_floating_point_v<typename Tout::value_type>
    );

This concept is now applied to one of my member functions:

class test_concept
{
template< typename U, typename Tin, typename Tout>
requires MyConditions <U, Tin, Tout>
static void test_routine(const U&, const Tin&, Tout& );
}

When testing:

std::vector<double> test{ };
std::vector<int> testi{ };
std::vector<double> test2{ };

test_concept::test_routine(testi, test, test2);

Using clang I get the error message that no matching were found, and a note saying:

note: because substituted constraint expression is ill-formed: missing 'typename' prior to dependent type name 'vector<int, allocator >::value_type' U::value_type

You don't need to manually check for the presence of the types. If they're not there, SFINAE will cause your concept to silently return false anyway. So:

template< typename U, typename Tin, typename Tout>
concept MyConditions =
    std::is_integral_v<typename T::value_type> &&
    std::is_floating_point_v<typename U::value_type> &&
    std::is_floating_point_v<typename Tout::value_type>;

But if you want to explicitly check for the types, here's the syntax:

template< typename U, typename Tin, typename Tout>
concept MyConditions =
    requires
    {
        typename U::value_type;
        typename Tin::value_type;
        typename Tout::value_type;
    } &&
    std::is_integral_v<typename T::value_type> &&
    std::is_floating_point_v<typename U::value_type> &&
    std::is_floating_point_v<typename Tout::value_type>;

You can also move all conditions into the requires :

template< typename U, typename Tin, typename Tout>
concept MyConditions =
    requires
    {
        typename U::value_type;
        typename Tin::value_type;
        typename Tout::value_type;
        requires std::is_integral_v<typename T::value_type>;
        requires std::is_floating_point_v<typename U::value_type>;
        requires std::is_floating_point_v<typename Tout::value_type>;
    };

Also you should prefer the standard concepts to the old traits:

template< typename U, typename Tin, typename Tout>
concept MyConditions =
    requires
    {
        typename U::value_type;
        typename Tin::value_type;
        typename Tout::value_type;
        requires std::integral<typename T::value_type>;
        requires std::floating_point<typename U::value_type>;
        requires std::floating_point<typename Tout::value_type>;
    };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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