C++20 中概念的语法

The syntax of a concept in C++20

我们如何将这个问题中的需求转化为一个概念

我尝试了以下操作:

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>
    );

这个概念现在应用于我的成员函数之一:

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

测试时:

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

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

使用 clang 我收到错误消息,指出未找到匹配项,并附有一条注释:

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

您不需要手动检查类型是否存在。如果它们不存在,SFINAE 将使您的概念无声地 return false 无论如何。所以:

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>;

但如果您想明确检查类型,语法如下:

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>;

您也可以将所有条件移动到 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>;
    };

此外,您应该更喜欢标准概念而不是旧特征:

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>;
    };