简体   繁体   English

概念可以要求constexpr值或函数吗?

[英]Can a concept require a constexpr value or function?

So, I'd like to do some advanced type-level hackery for which I would really like to be able to write a concept that requires a type to have a constexpr int value associated with it, which I can use later in the same concept as the integer std::array template parameter. 因此,我想做一些高级的类型级别的黑客,我真的很想能够编写一个概念,要求一个类型具有与之关联的constexpr int值,我以后可以在同一概念中使用作为整数std::array模板参数。

It's possible to write 有可能写

template<typename T>
concept bool HasCount = requires {
    typename T::count;
};

but this is not what I want; 但这不是我想要的; I would like T::count to be a static constexpr int . 我希望T::count是一个static constexpr int However, the code (not even including the needed constexpr ) 但是,代码(甚至不包括所需的constexpr

template<typename T>
concept bool HasCount = requires {
    int T::count;
};

does not compile with "error: expected primary-expression before 'int'" on GCC 7.3.0. 不会在GCC 7.3.0上编译为“错误:'int'之前的预期主表达式”。

Another failed attempt: it's possible to write this, which would require static int T::count() : 另一个失败的尝试:可以编写此代码,这需要static int T::count()

template<typename T>
concept bool HasCount = requires {
    {T::count()} -> int;
};

but not this, which is what I want: 但不是这个,这就是我想要的:

template<typename T>
concept bool HasCount = requires {
    {T::count()} -> constexpr int;
    {T::count() constexpr} -> int; // or this
    {constexpr T::count()} -> int; // or this (please forgive me for fuzzing GCC instead of reading the manual, unlike perl C++ is not an empirical science)
};

So, I'd like to know if it's in any way possible to require a concept expression to be constexpr-qualified, or if not if there's a reason why it can't be possible, or if it's just not included in the specification. 因此,我想知道是否可以以某种方式要求概念表达式符合constexpr的要求,或者是否没有可能的原因,或者它是否未包含在规范中。

In theory, this is possible by requiring T::count to be a valid expression, and requiring that it's valid to use T::count in a context that requires a constant expression. 从理论上讲,这可以通过要求T::count是有效的表达式,并要求在需要常量表达式的上下文中使用T::count是有效的。 For example: 例如:

#include <type_traits>
#include <utility>

template<int> using helper = void;

template<typename T>
concept bool HasCount = requires {
    // T::count must be a valid expression
    T::count;
    // T::count must have type int const
    requires std::is_same_v<int const, decltype(T::count)>;
    // T::count must be usable in a context that requires a constant expression
    typename ::helper<T::count>;
};

struct S1 {
    static constexpr int count = 42;
};
static_assert(HasCount<S1>);

struct S2 {
    int count = 42;
};
static_assert(!HasCount<S2>);

struct S3 {
    static constexpr double count = 3.14;
};
static_assert(!HasCount<S3>);

but in practice, the implementation of concepts in GCC rejects this program : 但实际上, GCC中概念的实施会拒绝该程序

<source>:20:16: error: invalid use of non-static data member 'S2::count'
 static_assert(!HasCount<S2>);
                ^~~~~~~~~~~~
<source>:18:17: note: declared here
     int count = 42;
                 ^~
<source>:20:16: error: invalid use of non-static data member 'S2::count'
 static_assert(!HasCount<S2>);
                ^~~~~~~~~~~~
<source>:18:17: note: declared here
     int count = 42;
                 ^~

(which fact I believe to be a bug .) 我认为是一个错误 。)

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

相关问题 无法从 constexpr function 的返回值初始化 constexpr 值 - Can't initialize constexpr value from return value of constexpr function 你可以调用一个 constexpr 函数来分配一个带有前向声明的 constexpr 值吗? - Can you call a constexpr function to assign a constexpr value with a forward declaration? constexpr函数不返回constexpr值吗? - constexpr function not returning constexpr value? 为什么成员 function 不能要求同一个 class 的 static constexpr 成员为真? - Why can't a member function require a static constexpr member of the same class to be true? 检查概念中的 constexpr - Checking for constexpr in a concept 为什么函数指针可以是`constexpr`? - Why can function pointers be `constexpr`? addressof()可以实现为constexpr函数吗? - Can addressof() be implemented as constexpr function? constexpr 函数可以包含标签吗? - Can a constexpr function contain a label? 为什么我不能在函数中使用constexpr值,但我可以在此值的范围内执行相同的操作? - Why can not I use constexpr value in function, but I can do the same in scope of this value? 如果结构化绑定不能是constexpr,为什么它们可以在constexpr函数中使用? - If structured bindings cannot be constexpr why can they be used in constexpr function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM