How to use the auto keyword correctly

Hi, LeafyCircuits here!

Comp Specs:
OS: Windows 7 Home Premium 64-bit
Compiler: MinGW v4.6.2
IDE: Code::Blocks v12.11

edit: ^ Just got MinGW and Code::Blocks

I've recently figured out a function that can erase a specific part of a vector that you specify with a string. It takes in a reference to a vector of char's, and the second arg is what you want to erase, as a string. I got the code working, but now I want to templatize it. However, I've run into this one problem. Take a look at this initialization in my function (don't worry about the rest of the code):
1
2
3
4
5
template <typename T>
bool erase_partof(vector<T> &vec, string search)
{
     vector<T>::iterator start_itr=vec.begin(), end_itr;
}


As you can see, even though I try initializing these iterators, the compiler doesn't recognize that I'm naming a type, since 'T' could be anything. I know that one way of fixing this would be to put 'typename' in front of this declaration, like so:
1
2
3
4
5
template <typename T>
bool erase_partof(vector<T> &vec, string search)
{
     typename vector<T>::iterator start_itr=vec.begin(), end_itr;
}


Although that does indeed work, that method is C++03, and I would rather learn how to use the C++11 method, which uses the 'auto' keyword. However, I'm not sure how to use auto. I have some idea as to how I'm going to use it, but I'm still confused. If anyone would care to explain, that would be wonderful, thanks. :)
Last edited on
You could write that as

1
2
     auto start_itr=vec.begin();
     decltype(start_itr) end_itr;


(btw, there is no C++09, the last revision before 11 was 03)
Last edited on
Cubbi said:
(btw, there is no C++09, the last revision before 11 was 03)


Lol, yeah, that's what I meant. I thought it was 09. Oh well.

I have yet to try what you stated, I'll get back to you once I do.
Just curious: which gcc version?
keskiverto said:
Just curious: which gcc version?


I do not have the latest gcc: I have the MinGW gcc v3.4.2
Guys/Gals, i am a definite n00b. i started trying to learn C++ as my first language about 3 days ago. Please be gentle with me lol. Having said that, I have a book that tries to explain how to use the auto keyword but the code in the lesson fails to compile both in Linux (using GCC compiler and in MS Visual C++ Studio.) Searching for an answer has brought me here. Now, i get line 1 of what Cubbi wrote. the decltype function in line 2 is what i think i am missing. i will look that up but i want to be pointed in the right direction. the code in the lesson is...

//Pre-Processor Directive
#include <iostream>

//Declaring namespace type
using namespace std;

int main ( )
{
auto Flag = true;
auto Number = 2500000000000;

cout << "Flag = " << Flag;
cout << ", sizeof(Flag) = " << sizeof(Flag) << endl;
cout << "Number = " << Number;
cout << " , sizeof(Number) = " << sizeof(Number) << endl;

return 0;
}

and the fail message i get is ..

moses@linux-8sfs:~/C++_Lessons/cpp> g++ -o online3.5 online3.5.cpp
online3.5.cpp: In function ‘int main()’:
online3.5.cpp:6:10: error: ‘Flag’ does not name a type
online3.5.cpp:7:10: error: ‘Number’ does not name a type
online3.5.cpp:9:26: error: ‘Flag’ was not declared in this scope
online3.5.cpp:11:28: error: ‘Number’ was not declared in this scope


please ignore the //comments. they are mine. It seems the compiler is looking for a type declaration (or i have done something that makes my auto irrelevant). if anyone could just point me in the right direction, i will do the work. thanks in advance -- the Diesel
Do you have c++11 turned on? Check your compiler settings btw why are you trying to.get the size of a boolean
Last edited on
LeafyCircuits wrote:
I do not have the latest gcc: I have the MinGW gcc v3.4.2

That is ancient by now. You should be able to get/upgrade to a newer gcc for MinGW. (MinGW means Minimalist GNU for Windows -- GNU programs compiled to run on Windows platform. GCC, GNU Compiler Collection, is one GNU program.)


@modiesel: Please, do not hijack threads.
@giblit i will take my response to another thread

@keskiverto my apologies. i saw a response that might have led to an answer to my question. it was not my intent to hijack (although i see that may be what i was doing).
keskiverto wrote
You should be able to get/upgrade to a newer gcc for MinGW.


Yeah, It's been a while. I don't have this decltype keyword, so yeah. I'll get back when I get the latest version.
Alright, I have the latest MinGW.
closed account (3qX21hU5)
IDE: Dev C++ v4.9.9.2 using C++11


I would also recommend upgrading your IDE away from Bloodshed Dev C++. This project is no longer being updated (Hasn't been in over 10 years I believe) so you definitely want to switch.

If you like Dev C++ you can switch to Orwell Dev-C++ which continued the project. To download that IDE go here http://orwelldevcpp.blogspot.com/

After that just check the compiler flag that enables C++11 and you should be good to go.
Zereo said:
I would also recommend upgrading your IDE away from Bloodshed Dev C++.


Actually, I already have. I'll update my comp specs from now on.
Topic archived. No new replies allowed.