小编Tik*_*454的帖子

如何在 if 语句中使用概念

我有一个检查类型是否可迭代的概念

template<typename T>
concept Iterable = requires(T t) {
    t.begin();
};
Run Code Online (Sandbox Code Playgroud)

由于重载问题,我无法在模板中使用它,所以我想做类似于以下的操作:

template<typename T>
void universal_function(T x) {
    if (x is Iterable)
        // something which works with iterables
    else if (x is Printable)
       // another thing
    else
       // third thing
}
Run Code Online (Sandbox Code Playgroud)

c++ templates c++-concepts c++20

2
推荐指数
1
解决办法
718
查看次数

标签 统计

c++ ×1

c++-concepts ×1

c++20 ×1

templates ×1