+ 6

Whats considered best practice in C++? And also what are some uses of lambda in c++ 17.

So this question stems from the fact that I have seen other tutorials where they mention that "using namespace std" is considered bad practice, at least using it globally, so its best used in locally. So I was wondering if I could see some examples of it being used locally. as for the second part Im just curious.

20th Feb 2018, 8:00 PM
Sideswipe
3 Réponses
+ 2
#include <iostream> using namespace std; /* This is a lambda c++ function example We declare a really simple function that is created "on fly" inside the main method, personally I like this kind of sintax because we can run functions that are domain specific in the methods of interest. Moreover It is really useful to refer at the already existing variables inside our method, see var1 and var2 that are defined outside the lamda function. */ int main() { int var1=1; int var2=2; //var1 and var2 refers to variables declared above, I have to pass only myVar as argument //auto let the compiler to automatically understand the data type auto lambdaFunc=[var1,var2](int myVar) { return (var1+var2)*myVar; }; cout<<lambdaFunc(1)<<endl; cout<<lambdaFunc(2)<<endl; cout<<lambdaFunc(5)<<endl; return 0; }
20th Feb 2018, 8:06 PM
Vishnu NS
Vishnu NS - avatar
+ 2
Avoid "using namespace std" This won't hurt u now, but in the future when u are part of a big project it can cause problems. If you’re putting the using statement into the header file, you’re aggravating the problem because the conflicts you will run into sooner or later will be in a module far, far away for no apparent reason until you find out that three layers down, one include file happens to include the file that contains the using directive and suddenly polluted whichever namespace the file contained with the whole contents of namespace std.
21st Feb 2018, 7:59 PM
illusion
illusion - avatar
0
Same name!
20th Feb 2018, 8:05 PM
Jax
Jax - avatar