Pilar Latiesa
Pilar Latiesa

Reputation: 695

Understanding what atomic constraints are

I've just learnt from a video of a presentation by Saar Raz that the following constraints:

template <typename T>
concept C = sizeof(T) > 1;


template <typename T>
concept D = sizeof(T) > 1 && sizeof(T) >= 4;

are ambiguous for overloading because the atomic constraints sizeof(T) > 1 occurring at C and D, respectively, are not equivalent.

They aren't because the standard says [templ.constr]:

Two atomic constraints are identical if they are formed from the same expression [...]

The key is that expression is in italics, referring to the grammar term, which is defined as [expr.comma]:

expression:

assignment-expression

expression , assignment-expression

I don't understand why an atomic constraint needs to involve an assignment. Why is so?

I must admit that the above code is best written by concept refinement, but I intuitively thought that that way of writing it was correct too.

Upvotes: 4

Views: 1227

Answers (1)

Barry
Barry

Reputation: 303741

I don't understand why an atomic constraint needs to involve an assignment. Why is so?

Emphasis mine.

It doesn't need to involve an assignment. It's just that expression is the top-level grammar term for expressions, that encompasses all the other kinds of expressions. sizeof(T) > 1 is an expression, as is sizeof(T) >= 4, as is sizeof(T) > 1 && sizeof(T) >= 4.

What this grammar definition means is that an expression is either an assignment-expression or another expression , assignment-expression. The grammar is hierarchically arranged based on what we consider to be operator precedence:

  • , has the lowest precedence, so the grammar pulls that one out first. That's what happens when we define expression recurisvely as expression , assignment-expression
  • = has the next lowest precedence, so we pull that one out next.
  • And then the grammar for assignment-expression takes us to logical-or-expression (next lowest precedence)
  • And then logical-and-expression, etc.

An assignment-expression need not actually involve an assignment. It's actually any kind of arbitrarily complex expression. All we know about it is that it definitely does not involve a , because we already pulled that one out.


Separately from all of that, the intent is that two atomic constraints are the same if they are literally the same expression in a source file. That is, constraint subsumption only applies to concepts.

Upvotes: 8

Related Questions