hamishmcn
hamishmcn

Reputation: 7981

Are there any workarounds to this spurious VS2010 C++ IntelliSense error

Here is some sample code that illustrates the problem.
It compiles and runs correctly, but the VS2010 editor IntelliSense complains about the hold variable and shows the following message

IntelliSense: a nonstatic member reference must be relative to a specific object

class tester
{
public:
    void test()
    {
        int outer[] = {1,2,3};
        int inner[] = {4,5,6};
        std::for_each(outer, outer + _countof(outer), [&](int o) {
            std::for_each(inner, inner + _countof(inner), [&](int i) {
                hold.push_back(i + o);
            });
        });
    }
private:
    std::vector<int> hold;
};

int main(int argc, char* argv[])
{
    tester().test();
    return 0;
}

NB: It is happy if there is only one level of for_each (just outer for example)

Has anyone else come across this, and if so, is there a way to change it slightly so that the IntelliSense is happy or am I stuck with red squiggly lines?

UPDATE: I have downloaded the preview of VS11 it is happy with the code - no squiggly lines, so at least it has been fixed for the next release.

Upvotes: 0

Views: 568

Answers (1)

Marc
Marc

Reputation: 408

If you are just looking for a workaround to placate VS2010's intellisense, you can capture hold in the outer lambda like this:

std::for_each(outer, outer + _countof(outer), [&](int o) {
    auto &hold = this->hold; // capturing hold to avoid intellisense reporting an "error"
    std::for_each(inner, inner + _countof(inner), [&](int i) {
        hold.push_back(i + o);
    });
});

Explicitly define hold's type or rename the variable for clarity as you feel is appropriate (using auto and name hiding due to laziness).

Upvotes: 2

Related Questions