auto keyword in c++4.3.2 - c++

My doubt is that does "auto" keyword works in C++4.3.2? I was writing a program to check the presence of a prefix in a word, I wrote something like this --
auto res = mismatch(prefix,word);
And when I compiled it gave the error--
res was not declared in scope
What should I use in place of "auto" in case if "auto" is not available in c++4.3.2.
Here u can see my full code-- http://paste.ubuntu.com/9351873/

#Surayans Tiwari
Return type of mismatch() is std::pair. Please refer following page and correct your usage.
http://en.cppreference.com/w/cpp/algorithm/mismatch

I have the same opinion with #KeithThompson that you are talking about using GCC 4.3.2 instead of using C++ 4.3.2.
Assume that I perceive correctly, the compilation error that you got is due to the auto type is not supported in GCC 4.3.2 yet.
According to the C++11 support in GCC reference,
https://gcc.gnu.org/projects/cxx0x.html
the auto-typed variables is supported since GCC 4.4.
If you wish to try the feature of auto type, you should use a newer GCC compiler.

Related

C++ "does not have a name type" error while using auto [duplicate]

All, recently i tried to use the new features supported by c++11, and i wrote such statement however the compiler ran failed.
auto x = 1;
the report error listed below:
D:\DEV\CBCppTest\main.cpp||In function 'int main()':|
D:\DEV\CBCppTest\main.cpp|22|warning: 'auto' changes meaning in C++11; please remove it [-Wc++0x-compat]|
D:\DEV\CBCppTest\main.cpp|22|error: 'x' does not name a type|
||=== Build finished: 1 errors, 1 warnings ===|
Why the last gcc version 4.7.0 on MinGW cannot support the this statement. But the compiler of vs10 passed. Could anyone know the reason for this issue?
"GCC provides experimental support for the 2011 ISO C++ standard. This support can be enabled with the -std=c++11 or -std=gnu++11 compiler options; the former disables GNU extension."
It comes from here: c+11 support
To explain what the compiler is actually complaining about: auto used to be an old C keyword, declaring that this variable has automatic storage. These keywords have little to do with the type system, they specify how variable are represented in memory: where they're stored (processor register vs. main memory / stack) and how the memory is reclaimed. auto means the variable is stored on the stack (though the processor may optimise it into a processor register) and the memory is automatically reclaimed when the variable goes out of scope – which is the right choice in almost any situation1 and thus the default, so virtually nobody ever used this old auto keyword. Yet C++03 still provided backwards compatibility for code that has it; today's compilers still want to support legacy code.
1Though often you want objects to reside on the heap, you'll still be accessing those through variables on the stack; C++ has its own methods of using heap-allocated memory (new, std::vector etc.), you don't need the unsafe C-style malloc stuff.
When compiling, you need to add -std=c++11 to g++ command line.
This is due to the feature not being enable by default by the GCC compiler. If you're on Codeblocks, go to Settings --> Compiler and enable the feature as shown - http://imgur.com/KrHx8nh
For me adding "-std=c++0x"" to g++ command line fixed the issue.

What's a quick simple bit of code to verify a given C++ compiler is reading source as C++11?

I think my compiler understands C++11, but maybe not. Instead of trying it on existing messes of source code which are buggy anyway, is there some simple "hello world" level snippet of source code I can try to compile, which if it does compile without error, proves the compiler is reading it as C++11?
Try this one,
auto f = [](){};
or write some code with rvalue reference.
Shortest thing possible:
[]{};
Is's a lambda-expression without argument list.
The Problem is that compiler usually don't support a new standard completely from the start. Meaning, they may support one c++11 feature, but not another.
However, as far as c++11 is concerned, I think VC++ is the only major compiler that doesn't fully support it, even though you may have to enable the c++11 mode manually. For g++ you e.g. have to supply the compiler flag -std=c++11 (or -std=gnu++11) - the same holds true for newer versions like c++14).

captured variable hides passed variable in lambda. how to unhide?

Consider this example:
int main()
{
int a = 100;
std::cout<<[=,&a](int a,int b){return a+b;}(99,1);
return 0;
}
The output is 101 instead of my expectation of 100.
I cant specify as so [&a,=] as it gives error.
How do i avoid the name hiding and refer to the parameter. i know changing the name is the option but i'm curious. also reference to standard will be helpful
EDIT:
i'm using gcc 4.7.1
EDIT:
here is the ideone link showing the demo. i used c++ 4.7.2 complier there
ideone
I could not find anything related to lambdas in the standard that would indicate your results are the expected behavior. I agree with Andy's comment that this is a bug in GCC. GCC 4.7.2 on Linux, GCC 4.7.2 from MinGW, and GCC 4.8.0 from MinGW produce the same results as in the question but VC++10 and VC++11 produce the expected results.
You should consider filing a bug report
Since the parameter doesn't have a scope that you can name, you can't use scope resolution operator to disambiguate, nor this or any other such means. Therefore, it's impossible to disambiguate this scenario the way you want. You'll just have to not give your variables horrible and clashing names.
How do I print out the outer a here, but in the inner scope?
int a = 1;
{
int a = 2;
cout << a << endl;
}
You either change the name of one of the variables - or you don't.
The same goes for your lambda.
(I apologize that I can't reference the standard like you requested - I don't have it.)

why i cannot use the auto keyword in the last version of gcc

All, recently i tried to use the new features supported by c++11, and i wrote such statement however the compiler ran failed.
auto x = 1;
the report error listed below:
D:\DEV\CBCppTest\main.cpp||In function 'int main()':|
D:\DEV\CBCppTest\main.cpp|22|warning: 'auto' changes meaning in C++11; please remove it [-Wc++0x-compat]|
D:\DEV\CBCppTest\main.cpp|22|error: 'x' does not name a type|
||=== Build finished: 1 errors, 1 warnings ===|
Why the last gcc version 4.7.0 on MinGW cannot support the this statement. But the compiler of vs10 passed. Could anyone know the reason for this issue?
"GCC provides experimental support for the 2011 ISO C++ standard. This support can be enabled with the -std=c++11 or -std=gnu++11 compiler options; the former disables GNU extension."
It comes from here: c+11 support
To explain what the compiler is actually complaining about: auto used to be an old C keyword, declaring that this variable has automatic storage. These keywords have little to do with the type system, they specify how variable are represented in memory: where they're stored (processor register vs. main memory / stack) and how the memory is reclaimed. auto means the variable is stored on the stack (though the processor may optimise it into a processor register) and the memory is automatically reclaimed when the variable goes out of scope – which is the right choice in almost any situation1 and thus the default, so virtually nobody ever used this old auto keyword. Yet C++03 still provided backwards compatibility for code that has it; today's compilers still want to support legacy code.
1Though often you want objects to reside on the heap, you'll still be accessing those through variables on the stack; C++ has its own methods of using heap-allocated memory (new, std::vector etc.), you don't need the unsafe C-style malloc stuff.
When compiling, you need to add -std=c++11 to g++ command line.
This is due to the feature not being enable by default by the GCC compiler. If you're on Codeblocks, go to Settings --> Compiler and enable the feature as shown - http://imgur.com/KrHx8nh
For me adding "-std=c++0x"" to g++ command line fixed the issue.

Does any compiler support constexpr yet?

I want to play with constexpr, does any compiler support it yet?
The Apache Stdcxx project has a nice table detailing which C++0x features are supported by which compilers. It's been updated on a regular basis and covers most of the modern C++ compilers.
According to that, only GCC 4.5 supports constexpr (note that that support may be experimental).
Between that list and what has been said in the comments, it appears the answer is "no."
As of July 2011, gcc 4.7 supports constexpr. You need to build it from svn though.
Agreed, g++ 4.5 and 4.6 support the keyword, but ignore the implications. I just compiled a simple factorial program (on both versions using -std=c++0x) with the line:
constexpr fact(int i) { return (i>1) ? fact(i-1)*i : 1; }
and it compiled and ran but when examining the asm source (-S option) it showed the function was called with the parameter instead of being determined by the compiler.
Usage of "constexpr" is really easy. Look at this piece of code:
constexpr int get_five(){
return 5;}
This function returns always 5, so it can be declared with "constexpr" keyword.
But factorial function returns value depending on argument, so its "output" is not always the same.