C++ 概念:如何使用 'concept' 检查模板化结构的 属性?
C++ Concepts: How to use 'concept' to check the property of a templated struct?
我目前正在试验新的 c++2a 'concepts' 功能。我下面的代码的目标是检查模板结构的一些 属性。作为第一个模板参数 ,如果没有 requires
表达式或手动指定模板参数,我很难使用这个概念。这没什么大不了的,但我喜欢 concept
符号,因为它很清晰。有办法解决这个问题吗?
编译器
gcc-g++-10.0 (GCC) 10.0.1 20200119 (experimental)
Copyright (C) 2020 Free Software Foundation, Inc.
编译命令
g++-10.0 -std=c++2a file.cc
代码
#include <concepts>
/// Struct has template arguments and a property that can be checked in a concept.
template <bool a> struct A {
constexpr static bool property() noexcept { return a; }
};
template <typename T, bool a> concept hasProp = std::is_same_v<T, A<a>> && A<a>::property();
template <bool a> requires hasProp<A<a>, a> void works(A<a> c) {}
template <bool a, hasProp<a> c> void deductionError(c d) {};
// This is a sketch of what I'd like to do:
// template <A<a, b> Class, bool a, bool b> concept hasProp = Class::property;
int main() {
A<true> a;
A<false> b;
works(a);
//works(b); //doesn't compile as the constraint is not fulfilled, which is desired.
//deductionError(a); // I get why this deduction error occurs, but is it possible to do this
// in a clean way using concepts without having so specify template arguments?
}
部分 class 模板特化来拯救:
template<class T>
struct HasProp : std::false_type{};
template<bool a>
struct HasProp<A<a>> : std::integral_constant<bool, A<a>::property()>
{};
template <class T>
concept hasProp = HasProp<T>::value;
template <bool a>
requires hasProp<A<a>>
void works(A<a>) {}
template<hasProp C>
void deductionError(C){} // no longer a deductionError
Demo
这确实(可能不必要地)将您的概念直接与 A
联系起来。
您可以改为这样做(如 ):
template <class T>
concept hasProp = T::property();
您真的需要 A
的搭档吗?
template <typename T> concept hasProp = T::property();
template <bool a> requires hasProp<A<a>> void works(A<a>); // ok
template <hasProp C> void deductionError(C); // also ok
如果您确实需要搭配,您可以将其完全添加到 hasProp
:
// unfortunately this is hard to generalize due to the non-type template parameter
template <typename T> struct is_A : std::false_type { };
template <bool b> struct is_A<A<b>> : std::true_type { };
template <typename T> concept hasProp = is_A<T>::value && T::property();
// ... and the rest works as before
template <bool a> requires hasProp<A<a>> void works(A<a>); // ok
template <hasProp C> void deductionError(C); // also ok
无论哪种方式,hasProp
都应该采用一种类型。
我目前正在试验新的 c++2a 'concepts' 功能。我下面的代码的目标是检查模板结构的一些 属性。作为第一个模板参数 requires
表达式或手动指定模板参数,我很难使用这个概念。这没什么大不了的,但我喜欢 concept
符号,因为它很清晰。有办法解决这个问题吗?
编译器
gcc-g++-10.0 (GCC) 10.0.1 20200119 (experimental)
Copyright (C) 2020 Free Software Foundation, Inc.
编译命令
g++-10.0 -std=c++2a file.cc
代码
#include <concepts>
/// Struct has template arguments and a property that can be checked in a concept.
template <bool a> struct A {
constexpr static bool property() noexcept { return a; }
};
template <typename T, bool a> concept hasProp = std::is_same_v<T, A<a>> && A<a>::property();
template <bool a> requires hasProp<A<a>, a> void works(A<a> c) {}
template <bool a, hasProp<a> c> void deductionError(c d) {};
// This is a sketch of what I'd like to do:
// template <A<a, b> Class, bool a, bool b> concept hasProp = Class::property;
int main() {
A<true> a;
A<false> b;
works(a);
//works(b); //doesn't compile as the constraint is not fulfilled, which is desired.
//deductionError(a); // I get why this deduction error occurs, but is it possible to do this
// in a clean way using concepts without having so specify template arguments?
}
部分 class 模板特化来拯救:
template<class T>
struct HasProp : std::false_type{};
template<bool a>
struct HasProp<A<a>> : std::integral_constant<bool, A<a>::property()>
{};
template <class T>
concept hasProp = HasProp<T>::value;
template <bool a>
requires hasProp<A<a>>
void works(A<a>) {}
template<hasProp C>
void deductionError(C){} // no longer a deductionError
Demo
这确实(可能不必要地)将您的概念直接与 A
联系起来。
您可以改为这样做(如
template <class T>
concept hasProp = T::property();
您真的需要 A
的搭档吗?
template <typename T> concept hasProp = T::property();
template <bool a> requires hasProp<A<a>> void works(A<a>); // ok
template <hasProp C> void deductionError(C); // also ok
如果您确实需要搭配,您可以将其完全添加到 hasProp
:
// unfortunately this is hard to generalize due to the non-type template parameter
template <typename T> struct is_A : std::false_type { };
template <bool b> struct is_A<A<b>> : std::true_type { };
template <typename T> concept hasProp = is_A<T>::value && T::property();
// ... and the rest works as before
template <bool a> requires hasProp<A<a>> void works(A<a>); // ok
template <hasProp C> void deductionError(C); // also ok
无论哪种方式,hasProp
都应该采用一种类型。