1

I've tried to compile elementary example:

#include <vector>
int main ()
{
    std::vector<int> testV;
    for (const auto& test : testV) 
    {   }
    return 0;
}

And I've received error:
test.cpp: In function 'int main()':
test.cpp:5:29: error: 'begin' was not declared in this scope
test.cpp:5:29: error: 'end' was not declared in this scope
test.cpp:5:29: error: unable to deduce 'const auto&' from '<expression error>'

Does STLport support const auto ?

EDIT: I'm using GCC 4.6
With 4.7 and more everything is ok.

Laser
  • 6,652
  • 8
  • 54
  • 85
  • 1
    it depends on your compiler, which version and what compiler are you using? – billz Aug 15 '13 at 11:16
  • `auto` is a feature of the compiler. Looks like the STLport library doesn't support non-member begin and end. Actually that may be a lie, try including ``. – BoBTFish Aug 15 '13 at 11:16
  • could you try `#include`? it should be included by ` ` already but you never know... – TemplateRex Aug 15 '13 at 11:18
  • 2
    Actually, I remember now it looks for the *member* versions first. Then the non-member ones (6.5.4). So it should work. – BoBTFish Aug 15 '13 at 11:18
  • @BoBTFish Thanks, I will try latest STLport version – Laser Aug 15 '13 at 11:21
  • 1
    @TemplateRex Please check section 6.5.4 of The Standard. This seems to be a very common misconception, but the member versions **are** looked for first. Then non-member versions via ADL (no normal lookup), with `std::` considered an associated namespace. ([pasted here](http://pastebin.com/iNz9mRjm), but bad formatting) – BoBTFish Aug 15 '13 at 11:29
  • 1
    @BoBTFish darn, you are right, [this simple test](http://coliru.stacked-crooked.com/view?id=c4ab5abcad5477e4869467fdd34d44b5-c96156d6cc95286981b0e9deef2eefae) confirms it – TemplateRex Aug 15 '13 at 11:32
  • @TemplateRex With gcc 4.7 and more everything is ok, but for 4.6 test failed. – Laser Aug 15 '13 at 12:15
  • @Pepelac it is a mysterious bug, see my answer to possible clues and solution. – TemplateRex Aug 15 '13 at 12:30
  • 1
    @BoBTFish The misconception is at least partly because older versions of C++0x before the official C++11 actually only looked for non-members (but the template `std::begin` and `std::end` just defer to the member functions anyway). – aschepler Aug 15 '13 at 13:55

1 Answers1

2

gcc 4.6 came out in the spring of 2011, was not without bugs in most C++11 features. Moroever, around the same time the rules for ADL lookup in range-for were also modified (note that this was prior to the official ratification of the C++11 Standard in the summer of 2011). See this Q&A for more details. It's probably not worth debugging this and the recommended course of action is to upgrade to a recent version of gcc (4.7 or preferably 4.8).

Community
  • 1
  • 1
TemplateRex
  • 69,038
  • 19
  • 164
  • 304