0

IIUC, I can't use auto variable as a function parameter with C++11, but it might be possible with C++14.

Is this true?

I believe whoever closed it is wrong - gcc-4.9 is not even C++11 compliant and definitely not C++14. So how the answer is helpful in this case?

Igor
  • 5,620
  • 11
  • 51
  • 103
  • @Barry, Sorry you are wrong. gcc-4.9 is not even C++11 compliant and so you can't possibly solve it with the post you referenced. – Igor May 03 '16 at 03:42
  • You should read the answers of the linked question. They answer your question (and the answer is no). – Barry May 03 '16 at 03:51

1 Answers1

1

In a lambda, yes.

A lambda expression can use auto as a function parameter type in C++14.

From the Microsoft Developer Network Lambada Expressions C++

In C++ 14, if the parameter type is generic, you can use the auto keyword as the type specifier. This tells the compiler to create the function call operator as a template. Each instance of auto in a parameter list is equivalent to a distinct type parameter.

auto y = [] (auto first, auto second)
{
    return first + second;
};

In C++ 14, ISO C++ still forbids it as a general function parameter. The compiler will give you errors.

In short, you cannot use it in general functions. You may only use it in a lambda function.

DarmaniLink
  • 126
  • 10
  • ok, so I still can't write something like int MyClass::Foo(auto param); right? it is available only in lambda? – Igor May 02 '16 at 21:08
  • If you know how to use lambadas and pointers, you can work around it. – DarmaniLink May 02 '16 at 21:10
  • Side note, from how you worded this, it sounded like you use auto everywhere you can. I would read [this](http://stackoverflow.com/questions/6434971/how-much-is-too-much-with-c11-auto-keyword) to get a general idea of how messy auto can be. It's fairly new, so it has it's own issues. – DarmaniLink May 02 '16 at 21:15
  • 3
    It's lambda, not lambada. [Lambada](https://en.wikipedia.org/wiki/Lambada) is something quite different. – Praetorian May 02 '16 at 21:22
  • Sorry, auto correct got me. Wrote this on my phone. – DarmaniLink May 03 '16 at 02:06
  • @DarmaniLink, no I am not using it everywhere. I was hoping to solve "virtual template" problem with auto. – Igor May 03 '16 at 03:45
  • Just kind of from the wording you were using, it was implied. – DarmaniLink May 03 '16 at 06:00
  • I don't get the "setting Y to the **return value of the lambda** part. The code sets `y` to the lambda, not its return value. `auto z = y(3,4)` would set `z` to the **return value of the lambda**. – MSalters May 03 '16 at 10:03