Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, July 3, 2018 4:32 PM
It is VC++ project in Visual Studio 2017 (v141) platform, v. 15.7.4. Window SDK is 10.0.17134.0, OS is Win 7 Pro SP1,
On declaring
void MyFn(auto@ paramName){...}
// or
void MyFn1(auto parName){...}
I am getting the message "auto is not allowed here."
Is this part of C++ 14 is not supported yet or is it my error in the project settings?
All replies (7)
Wednesday, July 4, 2018 12:32 AM âś…Answered
On 7/3/2018 8:17 PM, Geoyar wrote:
After my VS 2017 Community refused to accept 'auto container', I google 'DeclaringC++ function parameter 'auto.'
One of answers I got at https://www.quora.com/Can-we-use-auto-in-function-parameter-declaration-in-C++ was:
'Yes, you can use auto in a function declaration in C++14 (see example bellow).
Compiled with: g++ main.cpp -std=c++14
Must be some kind of GCC extension. Clang rejects the example, so does MSVC. And I don't see any mention of such parameters in the standard.
Tuesday, July 3, 2018 9:42 PM
On 7/3/2018 12:32 PM, Geoyar wrote:
It is VC++ project in Visual Studio 2017 (v141) platform, v. 15.7.4. Window SDK is 10.0.17134.0, OS is Win 7 Pro SP1,
On declaringvoid MyFn(auto@ paramName){...} // or void MyFn1(auto parName){...}I am getting the message "auto is not allowed here."
Is this part of C++ 14 is not supported yet or is it my error in the project settings?
What part of C++14 do you believe allows such a declaration? This doesn't look like valid C++ to me, under any version of the standard. Are you perhaps thinking of generic lambdas?
Wednesday, July 4, 2018 12:28 AM
0
This is replay to Igor Tandetnik.
You wrote:
>What part of C++14 do you believe allows such a declaration? This doesn't look like valid C++ to >me, under any >version of the standard. Are you perhaps thinking of generic lambdas?
I read the declaration with auto in C++ 17 STL Cookbook by Jacek Calowicz, ch.4 Lambda Expressions.
The code was:
static auto consumer (auto &container)
{
return [&] (auto value) {container.push_back(value);};
}
After my VS 2017 Community refused to accept 'auto container', I google 'DeclaringC++ function parameter 'auto.'
One of answers I got at https://www.quora.com/Can-we-use-auto-in-function-parameter-declaration-in-C++ was:
'Yes, you can use auto in a function declaration in C++14 (see example bellow).
Though I suspect it's some sort of templating shorthand. Note the
addresses of the 'i' static variables are different between the Call
that takes a functor and one that takes a function pointer.
As towards this being good or bad, that's what I'm online looking up right
now - my gut tells me that one declaration (that is pretty easy to
read) is better than more than one. Though, again, note that these are
different functions, so just be mindful of side effects, such as the
statics in the example bellow.
Compiled with: g++ main.cpp -std=c++14
Copy of the code:
#include <iostream>
#include <functional>
class Functor
{
public:
void operator()()
{
std::cout << "Functor operator called." << std::endl;
}
};
void Function()
{
std::cout << "Function called." << std::endl;
}
void Call( auto & fp )
{
static int i;
std::cout << "Unified calling..." << &i << std::endl;
fp();
}
int main( int argc, char ** argv )
{
Functor functor;// = new Functor;
std::function< void() > function = Function;
std::cout << "Begin testing..." << std::endl;
Call( functor );
Call( function );
std::cout << "End testing." << std::endl;
/*
output:
Begin testing...
Unified calling...0x100402080
Functor operator called.
Unified calling...0x100402090
Function called.
End testing.
*/
return 0;
}
Other three replies are saying that parameter auto is in ISO tech proposal but not in standard yet, and that auto parameter allowed only in lambda expressions.
But it seems some people use it in plain C++ functions.
Wednesday, July 4, 2018 6:24 AM
Hi,
thanks for posting here.
>>Other three replies are saying that parameter auto is in ISO tech proposal but not in standard yet, and that auto parameter allowed only in lambda expressions.
But it seems some people use it in plain C++ functions.
As the MSDN document says, the auto keyword is a simple way to declare a variable that has a complicated type. It directs the compiler to use the initialization expression of a declared variable, or lambda expression parameter, to deduce its type. To use the auto keyword, use it instead of a type to declare a variable, and specify an initialization expression. A parameter or template argument cannot be declared with the auto keyword.
Hope this could be help of you.
Best Regards.
Baron Bi
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].
Wednesday, July 4, 2018 7:50 PM
Thank you for replay. All is clear now.
Wednesday, July 4, 2018 7:53 PM
Thanks for replay.
It is not in ISO C++ now.
I winder how these two guys made it compiled.
Tuesday, July 10, 2018 12:01 AM | 1 vote
Just info:
I downloaded MinGW-64 v. 3.0.4 2018-06-o4 and set it as a toolcet for CDT 9.5 Eclipce Photon.
The code
auto Mlt(auto a, auto b)
{
return a*b;
}
int main()
{
.....................
auto a = 3;
auto b = 7;
auto c = Mlt(a, b);
.....................
return 0;
}
Was compiled, built, and ran just fine.
That is happening because this version MinGW (GCC 8.1) supports in advance some feature of upcoming C++ standard, C++ 2a as they call it;
To compile this code without warnings you have to add compiler option -fconcepts. If this option is not included, you are getting warning that it is needed, but the code compils anyway.
So, function parameters with auto are not in C++ 17,but supposed to be in C++ 20, and are in GCC 8.1.