[英]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.