C++0x support in VC10 (VS2010)

15 February, 2010 § 4 Comments

I’ve been using Visual Studio 2010 since the Beta 2 release and just a couple days ago installed the Release Candidate.

VC10 has support for some of the features of C++0x and I wanted to get a head start on playing with the features so that when we switch production environments at work I will be ready to start taking advantage.

While getting to know VC10 and C++0x, I’ve noticed a couple low-severity issues that I wanted to point out:

Lambda Functions

Lambda functions allow you to create an inline functor. They don’t supply any new capabilities of the language, but they cut out a lot of noise. Here is an example usage:

std::vector<int> numbers;

numbers.push_back(1);
numbers.push_back(2);

std::for_each(numbers.begin(), numbers.end(), [](int number){ std::cout << number << std::endl; });

Pre-C++0x, you would have had to write this:

struct Functor {
  void operator()(int number) {
    std::cout << number << std::endl;
  }
};

std::vector<int> numbers;
numbers.push_back(1);
numbers.push_back(2);

std::for_each(numbers.begin(), numbers.end(), Functor());

The thing that I have noticed is that if you use using-declarations throughout your code, not all of them get captured from inside of the lambda. For example, this does not work:


void exampleFunction( const std::vector<int>& numbers ) {
using std::cout;
using std::endl;

std::for_each(numbers.begin(), numbers.end(), [](int number){ cout << number << endl; });

For some reason, endl is not captured, and the VC10 compiler throws an error. However, either of these do work:


void exampleFunction2( const std::vector<int>& numbers) {
  std::for_each(numbers.begin(), numbers.end(), [](int number){ std::cout << number << std::endl; });
}

// or

using namespace std;
void exampleFunction3( const std::vector<int>& numbers) {
  std::for_each(numbers.begin(), numbers.end(), [](int number){ cout << number << endl; });
}

Also, if the lambda-return-type-clause is not specified, then implicit type conversion warnings do not get generated:


std::deque<int> integers;
std::transform(numbers.begin(), numbers.end(), std::front_inserter(integers), [](int number){
  return number > 0 ? number / 2.0 : number * 2;  // no warning here
});

std::transform(numbers.begin(), numbers.end(), std::front_inserter(integers), [](int number) -> int {
  return number > 0 ? number / 2.0 : number * 2;  // warning C4244: 'return' : conversion from 'double' to 'int', possible loss of data
});

It doesn’t seem like the type inference is doing a great job with the compiler warning here. I would recommend explicitly specifying the lambda-return-type-clause to get full type checking by the compiler.

Tagged: , , ,

§ 4 Responses to C++0x support in VC10 (VS2010)

  • A.J. says:

    Hey Hey Jared,

    So it took some time to get VS 2010 going. Now that it is I tried out the code segments you mentioned. That one glitch about using statements being captured from inside the lambdas; I got that too. The following code does work (in VS 2010):
    std::for_each(numbers.begin(), numbers.end(), [](int number){ using std::cout; using std::endl; cout << number << endl; });

    I don't know yet whether using statements do work in lambdas yet so I can't say for sure.

  • A.J. says:

    Hey Hey Jared,

    I tested to see if Explicit Virtual Function Overrides (http://en.wikipedia.org/wiki/C%2B%2B0x#Explicit_virtual_function_overrides) worked -> they didn’t:
    class A
    {
    public:
    virtual void virtMeth() {}
    };

    class B : public A
    {
    public:
    virtual void virtMeth [[override]] () {}
    };

    Oh well at least I still got SAL (http://msdn.microsoft.com/en-us/library/ms235402.aspx) :-)

  • msujaws says:

    To reply to your first comment:

    If you do a ‘using namespace std;’ outside of the containing function, then this will all work.

    To reply to your second comment:
    I hadn’t played with Virtual Function Overloads. The Wikipedia section mentions the possibility that the base class will get modified which can have an unexpected effect on the derived class. That was interesting, as I hadn’t thought of that scenario before.

    I briefly looked at SAL, but I didn’t see a mention of doing the same Explicit Virtual Function Overrides with SAL. Is there a post that shows how to do that with SAL?

  • [...] C++0x support in VC10 (VS2010) February 2010 3 comments 3 [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading C++0x support in VC10 (VS2010) at JAWS.

meta

Follow

Get every new post delivered to your Inbox.

Join 100 other followers