Concept-Lite如何与可变参数模板进行交互? [英] How will Concepts-Lite interact with variadic templates?

查看:193
本文介绍了Concept-Lite如何与可变参数模板进行交互?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Going Native 2013 中观看了Bjarne Strustrup的演讲>并且他为下一个概念提供了以下示例 - C ++的精简特性。

I watched Bjarne Strustrup's talk in Going Native 2013 and he gives the following example for the upcoming concepts-lite feature of C++.

void sort(Container& c); // terse notation

// Expands to
template <Container __Cont>
  void sort(__Cont& c); // shorthand notation

// Expands to 
template <typename __Cont>
  requires Container<__Cont>()
    void sort(__Cont & c);

我的问题是如何使用可变参数模板?

My question is how will this work with variadic templates?

说我想使用 Comparable 概念定义一个可变参数最大将接受以下语法?

Say I want to define a variadic maximum function using a Comparable concept. Will the following syntax be accepted?

auto maximum(Comparable a)
{
     return a;
}

auto maximum(Comparable c, Comparable... rest)
{        
    return std::max(a, maximum(rest...));
}

如果会, Comparable ... 意味着参数包中的所有元素都是相同类型,或者只是它们都是 Comparable 类型,因此包可以包含 int string ? (它们都是可比的,但彼此不相同)

If so will Comparable... mean that all the elements in the parameter pack are the same type or just that they are all Comparable types so that the pack may include both int and string? (which are both comparable but not to each other)

好奇心想知道。

推荐答案

我相信terse格式的意图是要求包中的所有元素必须是相同的类型。这是从n3701&s; 5.3中的例子派生的,它说明

I believe the intention of the terse format is to require all elements in the pack must be of the same type. This is derived from the example in n3701 §5.3, which states that

void sort(Random_access_iterator p, Random_access_iterator q);

应等效于

template<Random_access_iterator __Ran>
void sort(__Ran p, __Ran q);

因为


默认情况下,如果对两个参数使用相同的约束参数
type name,那么这些参数的类型必须相同
我们选择重复使用受约束的参数类型名称imply
相同类型,因为(在大多数环境中)是最常见的情况,
在一个作用域中使用两次的标识符将是奇怪的有两个不同的
含义,这里是优化简洁符号的最简单的情况。

"By default, if you use the same constrained parameter type name for two arguments, the types of those arguments must be the same. We chose to make repeated use of a constrained parameter type name imply "same type" because that (in most environments) is the most common case, it would be odd to have an identifier used twice in a scope have two different meanings, and the aim here is to optimize for terse notation of the simplest case."

但我不知道如何可以扩展到你的 maximum 与当前('14)模板语法。在n3701中提议改变标准字,它只讨论了简单的情况

But I don't see how it can be expanded into your maximum with the current ('14) template syntax. In proposed change of standard wording in n3701 it only talked about the simple case


§ 7.1.6.5约束类型说明符[dcl.spec。 constraint]



...

§7.1.6.5 Constrained type specifiers [dcl.spec.constrained]

...

第一次使用 concept-name 范围内的 partial-concept-id
名称绑定到占位符类型,以便后续使用相同名称引用
相同类型。

The first use of concept-name or partial-concept-id within a scope binds that name to the placeholder type so that subsequent uses of the same name refer to the same type.

但它不能解释可变参数的交互。也许需要等待第三修订版来澄清这一点。

But it doesn't explain the interaction of variadics. Maybe need to wait for the 3rd revision to clarify this.

这篇关于Concept-Lite如何与可变参数模板进行交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆