9

From this question, it is clear that auto cannot be used as function argument. My question is why return type is allowed as auto but function arguments are not ?

auto function(auto data)
{
    //DOES something
}

Since, there are many benefits of auto coming in c++1z, then why not this ?

Sahib Yar
  • 1,030
  • 11
  • 29
  • 2
    It (sort of) is; it's called a *template*. – WhozCraig Aug 11 '17 at 10:32
  • 1
    I know about `template`, but I am asking why `auto` not allowed ? – Sahib Yar Aug 11 '17 at 10:32
  • 1
    The other question predates C++17 by quite a bit. This is perfectly valid in C++17, provided that a few other requirements are met, but the gist of it that this is valid C++17, subject to a few restrictions, and, at least, gcc 7.1 is perfectly happy with it. – Sam Varshavchik Aug 11 '17 at 10:33
  • My point was, you can do it without `auto` until the language supplies what you seem to be seeking (and it does, as Sam pointed out, in C++17, subject to restrictions). – WhozCraig Aug 11 '17 at 10:34
  • why we can just ignore `template` and use `auto` as a whole ? – Sahib Yar Aug 11 '17 at 10:36
  • as [this](https://stackoverflow.com/questions/18135593/function-templates-vs-auto-keyword) also says, why we can't just ignore `template` and use `auto` as a whole ? – Sahib Yar Aug 11 '17 at 10:37
  • 3
    @SamVarashavchik: I'm pretty sure this is not valid c++17. – MikeMB Aug 11 '17 at 11:00
  • @SamVarshavchik This is not valid in C++17. gcc has an experimental implementation of the Concepts TS, which is what makes this legal. – Baum mit Augen Aug 11 '17 at 11:02

2 Answers2

13

This syntax was proposed in the Concepts TS, which did not make it into C++17 for various reasons.

Despite some critique I've outlined below, it has been added in C++20.


Note: the following part of the answer has been made obsolete by merging P1141R2 into the standard. I'll leave it here for reference.

However, even if we finally get Concepts in the next iteration (probably C++20), it is not clear the desired syntax will make it into the standard. In this paper, Richard Smith and James Dennett critique the so called "Terse template notations", saying

The Concepts TS introduces too many syntaxes for a function template declaration. Some of those syntaxes have no clear, consistent syntactic marker for templates, which is important semantic information for the reader of the code (remembering that code is read vastly more than it is written).

They go on to propose a different syntax to achieve the goal, while keeping template declarations more consistent:

=> Require an explicit sigil to declare a function to be a template

Replace

void f(auto a) {}
template<typename T> void g(auto a) {}

with

template<...> void f(auto a) {}
template<typename T, ...> void g(auto a) {}

The ​...​ sigil in the ​template-parameter-list indicates that additional template parameters will be inferred from the declaration.

So Tl;dr: There are a bunch of reasons related to the standardization procedure why we don't have it in C++17, and there are more reasons why we probably won't ever get it. If I understood the paper correctly, the key point being that every template should be introduced with a template keyword.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
7

There is already an exhaustive answer, I just want to add my two cents by giving a more handwavy answer....

In general auto does not stand for "this could be any type" but it just means that the compiler can deduce the type. Thus the two autos in

auto function(auto data) {
    //...
    return x;
}

are quite different. Seeing this definition alone, the compiler can deduce the return type. On the other hand the auto on the parameter cannot be deduced unless the compiler sees how function is called. Moreover, there need to be different versions of function. While it is almost trivial to figure out the type of x, making the second auto work requires to turn function into a template which requires new rules and inevitably introduces new quirks to the language.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • How does it introduce new quirks when equivalent template code would be doing the same thing? – Andrew Jan 14 '22 at 18:11
  • @Andrew I dont remember what I was thinking when writing this ;). Consider that it was written back in 2017 and targets C++17, now that C++20 is a thing, the answer feels a bit outdated indeed – 463035818_is_not_an_ai Jan 14 '22 at 18:36