1

I know that the use of auto keyword can automatically deduce the type of the variable from the Rvalue. Then why does the following function snippet in my code have a compilation error?

auto getName(auto str = "John Doe") {
    return str;
}

The compilation error is 'auto' not allowed in function prototype. I googled a bit and I think auto can not be used in the function prototypes. Why So?

  • 1
    Does this answer your question? [arrow operator (->) in function heading](https://stackoverflow.com/questions/22514855/arrow-operator-in-function-heading) – Stephen Newell Nov 06 '20 at 05:22
  • 1
    If you ever upgrade to c++20, this code becomes valid, see https://stackoverflow.com/a/60355539/2466431 – JVApen Nov 06 '20 at 07:03
  • Does this answer your question? [Is there a way to pass auto as an argument in C++?](https://stackoverflow.com/questions/29944985/is-there-a-way-to-pass-auto-as-an-argument-in-c) – JVApen Nov 06 '20 at 07:03

1 Answers1

6

You can use auto in a lambda expression, but not a normal function.

To get the same effect, you can define a function template instead:

template <class T>
T getname(T input = "John Doe") {
    return input;
}

But be aware that this default value for the argument will only work for types that can actually be initialized from a string literal.

Oh, and as an aside, names starting with str are reserved, so it would be better to use a different name.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • `str` being reserved is new to me. please elaborate. – Raildex Nov 06 '20 at 05:30
  • C99, §7.26: "The following names are grouped under individual headers for convenience. All external names described below are reserved no matter what headers are included by the program.", §7.26.11: "Function names that begin with **str**, **mem**, or **wcs** ..." Technically, as it stands right now, you're *probably* all right, as any added name would start with **str**, but be followed by something else to create a complete name. But I'd avoid it nonetheless. – Jerry Coffin Nov 06 '20 at 05:39
  • This is a C++ question, not a C one, so quoting the C spec is not helpful. As an aside, the C17 spec has something along the lines of "Function names that begin with str, mem, or wcs and a lowercase letter may be added to the declarations in the header." That says nothing about them being reserved. – 1201ProgramAlarm Nov 06 '20 at 05:44
  • @1201ProgramAlarm: The C++ spec cites the C spec as its base document, so yes, it's entirely applicable in this case. In particular, the C++ standard specifies that "the contents and meaning of the header `` are the same as the C standard library header ``. That encompanses the full description of the header--everything it is required to include *and* the other names it reserves. – Jerry Coffin Nov 06 '20 at 05:52
  • 1
    That only says anything about function names anyway, and `str` isn't a function in asker's code... – Nathan Pierson Nov 06 '20 at 06:15
  • 2
    After looking both specs over, I think it is a bit more complicated than this. The `[extern.names]` section in the C++ spec explicitly reserves those names from the C library. The C future directions section you cite specifically mentions "function names" and that the reserved names "begin with str, mem, or wcs and a lowercase letter". A local variable named `str` would not be reserved. – 1201ProgramAlarm Nov 06 '20 at 06:37
  • @NathanPierson: And do you believe that two things named (for example) `stra` won't conflict just because one of them is a function and the other a variable? And Note that what the standard says is *not* that "functions with these names are reserved", but that the names themselves are reserved, so the fact that one is a function and the other a variable is of precisely zero relevance. – Jerry Coffin Nov 06 '20 at 08:21
  • 1
    Now, it is true that its being a local variable does save it from conflicting--that's why I was careful to not say anything like: "this name is reserved, so using it causes undefined behavior", only that names like it are reserved, so I wouldn't use it. – Jerry Coffin Nov 06 '20 at 08:22