auto as function parameter and return type.

292 views
Skip to first unread message

teco...@gmail.com

unread,
Jun 23, 2017, 1:01:01 PM6/23/17
to ISO C++ Standard - Discussion
I start to use the g++ 7.1 that still in test phase and I thought the auto statement to function parameters and its returns were already approved by c++ committee whereas it It has been discussed since c++14.

 however when I got the follow warning from compiler:

I expected to use auto statement to function parameters as shown below:

warning: ISO C++ forbids use of auto in parameter declaration [-Wpedantic]|
auto function(auto Var) {
    (...)    
}

This flexibility is important in my opinion. Furthermore, if it is really approved, this kind of statement may substitute, at least in part, the verbose, not elegant and not so clear template statement as shown below?

template <typename T>
T function(T Var){ 
  (...)
} 

If it can't, i think it could be proposed.

Jens Maurer

unread,
Jun 23, 2017, 1:33:54 PM6/23/17
to std-dis...@isocpp.org
On 06/23/2017 07:01 PM, teco...@gmail.com wrote:
> I start to use the g++ 7.1 that still in test phase and I thought the auto statement to function parameters and its returns were already approved by c++ committee whereas it It has been discussed since c++14.
>
> however when I got the follow warning from compiler:
>
> I expected to use auto statement to function parameters as shown below:
>
> ||
> warning:ISO C++forbids useof ‘auto’inparameter declaration [-Wpedantic]|
>
> auto function(auto Var) {
> (...)
> }

> This flexibility is important in my opinion. Furthermore, if it is really approved, this kind of statement may substitute, at least in part, the verbose, not elegant and not so clear template statement as shown below?
>
> template <typename T>
> T function(T Var){
> (...)
> }

This at least clearly expresses that the return type is the
same type as the parameter type. With "auto", that's not
obvious.

Jens

Thiago Macieira

unread,
Jun 23, 2017, 1:59:39 PM6/23/17
to std-dis...@isocpp.org
On Friday, 23 June 2017 10:01:01 PDT teco...@gmail.com wrote:
> I expected to use auto statement to function parameters as shown below:
>
> warning: ISO C++ forbids use of ‘auto’ in parameter declaration [-Wpedantic
> ]|
>
> > auto function(auto Var) {
> > (...)
> > }
>
> This flexibility is important in my opinion.

You're getting the warning because the above is not part of C++14. It is part
of the Concepts TS, though. So if you compile with
-std=c++1z -fconcepts

You'll get no warning.

> Furthermore, if it is really
> approved, this kind of statement may substitute, at least in part, the
> verbose, not elegant and not so clear template statement as shown below?
>
> template <typename T>T function(T Var){
>
> > (...)}
>
> If it can't, i think it could be proposed.

It's already proposed and it does not mean what you wrote. It means, instead:

template <typename T> auto function(T var)
{
....
}

The important difference is what Jens said: the return type is not necessarily
the same as the parameter type.

--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
Software Architect - Intel Open Source Technology Center

Tom Honermann

unread,
Jun 23, 2017, 4:16:29 PM6/23/17
to std-dis...@isocpp.org
As Thiago mentioned, this (well, something very much like it as he pointed out) is currently included in the Concepts TS.  The feature is controversial however and I suspect will be the subject of some attention at the upcoming meeting.  See [1].

Tom.

[1]: http://htmlpreview.github.io/?https://github.com/tahonermann/std-proposals/blob/master/p0696r0.html
Reply all
Reply to author
Forward
0 new messages