C++20模板模板概念语法

有了概念,C++20提供了很好的语法,比如

template<typename T>
concept SomeConcept = true; // stuff here


template<typename T>
requires SomeConcept<T>
class Foo;

template<SomeConcept T>
class Foo;

其中限制类的两种概念方式是等效的,但后者更简洁。

如果我现在有一些模板模板概念,例如

template<template<typename> typename T>
concept SomeOtherConcept = true; // stuff here

template<template<typename> typename T>
requires SomeOtherConcept<T>
class Foo;

我不知道没有要求子句的非详细(简洁/简短)语法,例如

template<template<typename> SomeotherConcept T>
class Foo;

template<template<SomeOtherConcept> typename T>
class Foo;

没有用,所以

声明这样一个模板模板类的正确语法是什么,对模板模板参数有概念限制?

回答

声明这样一个模板模板类的正确语法是什么,对模板模板参数有概念限制?

编写依赖于模板模板参数或非类型模板参数的约束的唯一方法是使用requires-clause。较短的类型约束语法仅适用于约束类型的概念(因此名称为type-constraint):

template <typename T> concept Type = true;
template <template <typename...> class Z> concept Template = true;
template <auto V> concept Value = true;

// requires-clause always works
template <typename T> requires Type<T> struct A { };
template <template <typename...> class Z> requires Template<Z> struct B { };
template <auto V> requires Value<V> struct C { };

// type-constraint only for type concepts
template <Type T> struct D { };

// abbreviated function template definitely only for type concepts
void e(Type auto x);


以上是C++20模板模板概念语法的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>