70

In C++20, we are now able to constrain the auto keyword to only be of a specific type. So if I had some code that looked like the following without any constraints:

auto something(){
  return 1;
}

int main(){
  const auto x = something();
  return x;
}

The variable x here is deduced to be an int. However, with the introduction of C++20, we can now constrain the auto to be a certain type like this:

std::integral auto something(){
  return 0;
}

int main(){
  const auto x = something();
  return x;
}

Doesn't this defeat the purpose of auto here? If I really need a std::integral datatype, couldn't I just omit the auto completely? Am I misunderstanding the use of auto completely?

2
  • 59
    std::integral isn't a type, it's a concept. The second version of the code is simply promising that whatever type something() returns, it will be a type that satisfies the concept std::integral. Nov 1, 2021 at 18:24
  • 3
    The main purpose of that syntax is for parameters, where it’s plainly useful; return types and ordinary variables are just a bonus. Nov 2, 2021 at 1:14

3 Answers 3

78

A constraint on the deduced auto type doesn't mean it needs to be a specific type, it means it needs to be one of a set of types that satisfy the constraint. Note that a constraint and a type are not the same thing, and they're not interchangeable.

e.g. a concept like std::integral constrains the deduced type to be an integral type, such as int or long, but not float, or std::string.

If I really need a std::integral datatype, couldn't I just omit the auto completely?

In principle, I suppose you could, but this would at the minimum lead to parsing difficulties. e.g. in a declaration like

foo f = // ...

is foo a type, or a constraint on the type?

Whereas in the current syntax, we have

foo auto f = // ...

and there's no doubt that foo is a constraint on the type of f.

6
  • 1
    Thank you, that clears things up. From what I understand, was this introduced so that the programmer sort of knows what to expect from a function that uses auto as a return type? This will surely make the code less bug-prone and easier to debug, is that correct?
    – beep_boop
    Nov 1, 2021 at 18:38
  • 9
    @MaheeppartapSingh Yes, concepts allow the programmer to make the intent much clearer, which is always nice. There are a bunch of additional benefits, e.g. function templates that are instantiated incorrectly can be caught more easily with concepts; otherwise it would require the definition to fail, which is unpleasant (produces bad error messages, e.g.)
    – cigien
    Nov 1, 2021 at 18:41
  • 5
    auto has given me some pretty interesting surprises over the years. Often it means I didn't get something quite right elsewhere and the additional diagnostics I could get with a constraint may catch an otherwise tricky runtime error or several pages of nigh-inscrutable template/overload resolution notes. Nov 1, 2021 at 18:50
  • 1
    Iirc, earlier versions of the "concepts lite" proposal/implementation did not have the auto in the syntax. I believe it is mostly for there for clarity and concepts are already their own syntactical category, similar to types.
    – Carsten S
    Nov 2, 2021 at 13:19
  • Man, I have several times thought it’d be great if my language of choice these days (TypeScript) had this feature. Nice to see I’m not alone in that!
    – KRyan
    Nov 3, 2021 at 3:05
36

If I really need a std::integral datatype, couldn't I just omit the auto completely?

No, because std::integral is not a type, it's a concept, a constraint on types (or if you will, a set of types rather than a single type).

Doesn't this defeat the purpose of auto here?

The original purpose of auto in C++11 is telling the compiler: Whatever type you deduce.*

With C++20, auto has an expanded use case - together with a concept, a constraint over types. auto still tells the compiler: Whatever type you deduce - but the deduction must also respect the constraint.

* - ignoring issues like constness, l/rvalue reference etc.

4
  • 2
    It's interesting that constraining gives more (or expanded) use cases. But I agree: constraining gives more power because it gives more guarantees. :-)
    – Pablo H
    Nov 2, 2021 at 14:18
  • @PabloH: Similar to the principle of Inheritance in Object Oriented Design. The “sub”-class has potentially more functionality than the parent or “super”-class. Nov 2, 2021 at 20:14
  • 1
    Re The original purpose of auto is telling the compiler: Whatever type you deduce. The original purpose of auto was to tell the compiler that an object had automatic storage (as opposed to register or static or extern). The auto keyword was rarely used because that was the default for variables at block scope. There was no reason to use auto SomeType foo when SomeType foo meant the same thing. This was what made it so easy to usurp the auto keyword in C++11 for other purposes. Nov 3, 2021 at 8:37
  • @DavidHammen: Edited to clarify we're talking about C++11-auto, not the earlier use of the keyword which is not related to this question.
    – einpoklum
    Nov 3, 2021 at 8:51
4

A concept often just move the error earlier in the compilation and makes code a bit more readable (since the concept name is a hint to the reader what you require from a type).

Rephrased:

It is rare you will ever use an auto variable in a way that it will work on every type.

For example:

auto fn(auto x) {
    return x++;
}

will not work if you do:

f(std::string("hello"));

because you can not increment std::string, the error is something like:

error: cannot increment value of type 'std::basic_string<char>'
    return x++;

If you change the function to:

auto fn(std::integral auto x) {
    return x++;
}

You will get an error like:

:6:6: note: candidate template ignored: constraints not satisfied [with x:auto = std::basic_string] auto fn(std::integral auto x) {

For a small example this, it does not matter a lot, but for real code often the fn would call fn2 that calls fn3... and you would get the error deep in the std/boost/... implementation file.

So in this way concepts move the error to the site of the first function call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.