Using C ++ lambda functions during variable initialization

I think many of you have this code somewhere:

int foo; switch (bar) { case SOMETHING: foo = 5; break; case STHNELSE: foo = 10; break; ... } 

But this code has some disadvantages:

  • You can easily forget the "break"
  • The variable foo not const, but it should be
  • It's just not pretty

So, I started wondering if there is a way to “improve” this type of code, and I got this little idea:

 const int foo = [&]() -> int { switch (bar) { case SOMETHING: return 5; case STHNELSE: return 10; ... } }(); 

Note: the first pair of brackets is optional, but MSVC ++ does not yet support this.

You can use the same trick with if-else, where the ternary operator will be too complicated, variables that must be passed in by pointers that will be initialized (for example, for DirectX functions), etc.

My questions:

  • Is there something wrong with this code that I have not seen?
  • Do you find it better the higher?
  • g ++ seems to be built into the function, but do you think all compilers will do it?

EDIT: this is what I mean by “DirectX Features”

 _xAudio2 = [&]() -> std::shared_ptr<IXAudio2> { IXAudio2* ptr = nullptr; if (FAILED(XAudio2Create(&ptr, xAudioFlags, XAUDIO2_DEFAULT_PROCESSOR))) throw std::runtime_error("XAudio2Create failed"); return std::shared_ptr<IXAudio2>(ptr, [](IUnknown* ptr) { ptr->Release(); }); }(); 
+7
c ++ lambda c ++ 11
source share
2 answers

This is a fairly common method in other languages. Almost every high-level function of a Schema is defined in terms of lambda that are invoked immediately.

In JavaScript, this is the core of a “module template,” for example

 var myModule = (function() { // declare variables and functions (which will be "private") return { // populate this object literal with "public" functions }; })(); 

Thus, an anonymous function is declared and immediately called, so any internal data is hidden, and only the external value is displayed outside.

The only drawback is that with random reading of the code, the return seem to be coming back from an external function (during the Yambda wars, Java had intense debate). But this is just what you need to get used to when your language has lambdas.

There are many language functions in an imperative language, such as C ++, that can benefit from being able to return a value (rather than as a void function). For example, if has an alternative, the tertiary operator expr ? a : b expr ? a : b .

In Ruby, almost all operators can be evaluated, so there is no need for a separate syntax where you can specify the return value. If C ++ worked this way, it would mean things like:

 auto result = try { getIntegerSomehow(); } catch (const SomeException &) { 0; } 
+3
source share

I see no reason to use a switching case in such a case. Any worthy compiler will generate as fast code with if statements as it does with a switch case.

 if(bar == SOMETHING) foo = 5; else if(bar == STHNELSE) foo = 10; 
0
source share

All Articles