Posts

Showing posts from March, 2018

Lambda Functions

Lambdas functions C++11 provides the ability to create anonymous functions, called lambda functions. It allows a function to be defined at the point where it’s needed in another expression. It is a function that we can write inline in our code to pass in to another function. We can use lambdas wherever a function object or a functor is expected. lambdas are more convenient than function objects because the tedium of writing boilerplate code for every function class (a constructor, data members and an overloaded operator() among the rest) is relegated to compiler. A lambda expression has the form : [capture] (parameters)->return-type {body} The [] construct inside a function call’s argument list indicates the beginning of a lambda expression. Capture Lists. Unlike an ordinary function which can only access its parameters and local variables, a lambda function can also access variables from the enclosing scope. There are two ways to capture variables with

C++ 11

Distinguish between () and {} A novel feature of braced initialization is that that it prohibits implicit narrowing conversions among built in types. double x,y,z; int sum { x+y+z } // Error, sum of doubles may not be expressible as int Initilization using parenthesis and “=” does’nt check for narrowing conversions, because that could break too much legacy code. int sum(x+y+z) // OK int sum = x+y+z // OK Another noteworthy characteristics of braced initialization is its immunity to C++’s most vexing parse. Widget W1(10) ; // Call Widget constructor with argument 10 Widget W2() ;   // Most Vexing parse ! Declares a function named W2 that returns a Widget.! Widget W3{} ; // Calls Widget Constructor with no arguments. During constructor overload resolution, braced initializers are matched to std::initlizer_list parameters if at all possible, even if other constructors offer better matches. class Widget { public: Widget(int i, bo

TCP vs UDP

Image
TCP UDP Both TCP and UDP are protocols used for sending bits of data – known as packets – over the internet. They both are built on top of Internet protocol. In other words, whether you are sending a packet via TCP or UDP, that packet is sent to an IP address. Transmission Control Protocol User Datagram Protocol TCP is a connection-oriented. UDP is a connectionless. Connection must be established prior to transmission. Connection less data is sent without setup. TCP is suited for applications that require high reliability and transmission time is relatively less critical. USP is suitable for applications that need fast, efficient transmission, such as games. TCP rearranges data packets in the order specified. UDP has no inherent order as all packets are independent of each other. The speed for TCP is slower than UDP. UDP is faster because error recovery is not at