smallB
smallB

Reputation: 17110

Invalid use of auto

In this code:

for ( ;(auto i = std::find(some_string.begin(),some_string.end(),'%')) != some_string.end();)
    {

    }  

I'm getting error from gcc 4.7.1:

error: invalid use of 'auto'|   

any ideas why? shouldn't that be correctly compiled?

Upvotes: 1

Views: 2644

Answers (2)

Phil Wright
Phil Wright

Reputation: 180

If this is in a loop, you'll only ever find the first '%'. You need to resume searching from i, not some_string.begin() to find subsequent '%'.

auto i = std::find(some_string.begin(), some_string.end(), '%'));
while (i != some_string.end()) {
    // Your code here.

    i = std::find(i, some_string.end(), '%'));  // Find next '%'.
}

Upvotes: 0

piokuc
piokuc

Reputation: 26164

I think it has nothing to do with auto. You just cannot declare variables in random places, for example this will not compile either - an equivalent of what you were trying to do, but without auto:

int main() {
  for ( ; (int i = 0) != 1; ++i)
    ;   
  return 0;
}

Upvotes: 6

Related Questions