简体   繁体   English

C++20中模板class的概念

[英]Concept of template class in C++20

I'm new in advanced usage of templates and concepts, so here is a liitle bit complex problem:我是模板和概念高级用法的新手,所以这里有一个有点复杂的问题:

  • I have some Traits concept of many traits for each of Source classes:我对每个Source类都有许多特征的Traits概念:

     template<typename _Traits> concept Traits = requires { std::same_as<std::decay_t<decltype(_Traits::token)>, std::string_view>; };
  • I have some template class that uses this concept to handle object_one with various traits (for example, half of Source classes returns object_one ):我有一些模板 class,它使用这个概念来处理具有各种特征的object_one (例如,一半的Source类返回object_one ):

     template <concepts::Traits _Traits> class Object_one_handler final { static std::string handle_object(const object_one& obj) {/*...*/} };
  • Then I have Objects_handlers concept of handlers for various objects from set {object_one, object_two, object_three} from various Sources with their Traits :然后我有Objects_handlers处理程序的概念,这些对象来自各种Sources的集合{object_one, object_two, object_three}及其Traits

     template<template <concepts::Traits _Traits> class _Objects_handlers, typename _Object> concept Objects_handlers = requires(const _Object& obj) { // has handle_object method { _Objects_handlers<???????>::handle_object(obj) } -> std::same_as<std::string>; };
  • Finally, I creating some database with specified as template parameter Object_handler :最后,我创建了一些指定为模板参数Object_handlerdatabase

     template<concepts::Objects_handlers _handler> class database {...};

(Actually all of concepts have additional requirements, but it doesn't matter here) (其实所有的概念都有额外的要求,这里无所谓)


So problem is in last Objects_handlers concept:所以问题出在最后的Objects_handlers概念中:

template<template <concepts::Traits _Traits> class _Objects_handlers, typename _Object>
concept Objects_handlers = requires(const _Object& obj)
{
  // has handle_object method
  { _Objects_handlers<???????>::handle_object(obj) } -> std::same_as<std::string>;
                      ^^^^^^^
};

I can't check _Objects_handlers method without template parameter (obviously) and I can't properly set the template parameter which must be one of Traits .我无法在没有模板参数的情况下检查_Objects_handlers方法(显然),而且我无法正确设置必须是Traits之一的模板参数。

How can I do that?我怎样才能做到这一点?

And actually it may be problem in usage of Objects_handlers in template of database class, so one more question: how to use it?而实际上可能是database Objects_handlers模板中Objects_handlers的使用有问题,所以又多了一个问题:如何使用?


PS It can be XY problem or not about concepts at all... Maybe composition with strategy pattern will be more usefull, but still want try to create this maybe useless, but workable concept. PS 这可能是 XY 问题,也可能根本不是关于概念的问题......也许与策略模式组合会更有用,但仍然想尝试创建这个可能无用但可行的概念。

Let's reduce this problem a lot.让我们大大减少这个问题。

template <typename T>
struct C {
    void f();
};

Now, your goal is to write a concept that takes any class template (eg C ) and checks that every specialization of it has a nullary member function named f .现在,您的目标是编写一个采用任何 class 模板(例如C )的概念,并检查它的每个特化都有一个名为f的无效成员 function 。

template <template <typename> class Z>
concept HasF = requires (Z<???> z) {
    z.f();
};

The problem is - class templates in C++ just don't work like this.问题是 - C++ 中的 class 模板不能像这样工作。 Even for a particular class template, like C , you can't require that every specialization has f .即使对于特定的 class 模板,如C ,您也不能要求每个特化都有f There's no way to ensure that like somebody, somewhere, didn't add:没有办法确保像某人一样,在某处,没有添加:

template <> struct C<std::vector<std::list<std::deque<int>>>> { };

All you can do is check that a specific type has a nullary member function named f .您所能做的就是检查特定类型是否具有名为f的无效成员 function。 And that's:那就是:

template <typename T>
concept HasF = requires (T t) { t.f(); };

The type-constraint syntax, template <Concept T> , is only available for concepts that constrain types, not concepts that constrain templates or values.类型约束语法template <Concept T>仅适用于约束类型的概念,不适用于约束模板或值的概念。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM