How will Concepts-Lite interact with variadic templates?

限于喜欢 提交于 2019-12-03 12:15:21

问题


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?

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...));
}

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)

Curious minds want to know.


回答1:


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);

should be equivalent to

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

because

"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."

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 Constrained type specifiers [dcl.spec.constrained]

...

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.




回答2:


I don't know if N3580 is the latest version of the concept paper but in 4.5.1 it seems to describe how type requirements can be used with variadic argument lists. It seems that

template <Comparable... T>
auto maximum(T... values) -> <return-type-goes-here>

would require that each of the arguments satisfies the Comparable requirement but there is no restriction that the types T... are all the same. This requirement would need to be imposed separately. Off-hand, the only way I'd know how to impose the same type requirement is the same approach as in C++11 but the concepts facilities may have a better approach.



来源:https://stackoverflow.com/questions/18811772/how-will-concepts-lite-interact-with-variadic-templates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!