Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How will Concepts-Lite interact with variadic templates?

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.

like image 828
Motti Avatar asked Sep 15 '13 11:09

Motti


1 Answers

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.

like image 195
kennytm Avatar answered Nov 15 '22 20:11

kennytm