Thursday, August 4, 2016

Why is Templates :-)

Templates

Why :-)

We use templates when we need functions/classes that apply the same algorithm to a several types. So we can use the same function/class regardless of the types of the argument or result.

In other words, a template is a mechanism that allows a programmer to use types as parameters for a class or a function. The compiler then generates a specific class or function when we later provide specific types as arguments. In a sense, templates provide static (compile-time) polymorphism, as opposed to dynamic (run-time) polymorphism. A function/class defined using template is called a generic function/class, and the ability to use and create generic functions/classes is one of the key features of C++.

We can think of a class template as a type generator. The process of generating types from a class template is called specialization or template instantiation.

In most of the cases, template instantiation is very complicated, but that complexity is in the domain of compiler writer, not the template user. Template instantiation takes place at compile time or link time, not at run time.
,,,

Specialization or Template Instantiaion

The process of generating types from a given template arguments is called specialization or template instantiation. For example, vector<int> and vector<Node*> are said to be a specialization of vector.

,,,


Generic Programming --Template

Polymorphism

OOP--class hierarchies & virtual functions

Templates are the foundation of generic programming. Generic programming relies on polymorphism. Though there are several differences between OOP (class hierarchies and virtual functions) and generic programming (templates), the major difference is:

  1. Generic (templates) : compile time resolution. 
     The choice of function invoked when we use is determined by the compiler at compile time.
  2. OOP (virtual functions) : run time resolution.
Generic programming lets us write classes and functions that are polymorphic across unrelated types at compile time. A single class or function can be used to manipulate objects of a variety of types. The standard library containers, iterators, and algorithms are examples of generic programming. We can use library classes and functions on any kind of type.

When we parameterize a class, we get a class template, and when we parameterize a function, we get a function template.


So, what do people actually use template for?
  • When performance is essential.
  • When flexibility in combining information from several types is essential.
But note that, the flexibility and performance come at the cost of poor error diagnostics and poor error messages.
,,,










Static Members and Variables


Each template class or function generated from a template has its own copies of any static variables or members.


Each instantiation of a function template has it's own copy of any static variables defined within the scope of the function.



Static members are defined as follows.

////TTT.h
#include <iostream>
using namespace std;

template<typename T>
class X
{
public:
  static T s;
};

template<typename T> T X<T>::s = 0;
template<> int X<int>::s = 3;
template<> char* X<char*>::s = "Hello";

int main()
{
  X<int> xi;
  cout << "xi.s= " << xi.s <<endl;/// xi.s = 10
 
  X<char*> xc;
  cout << "xc.s= " << xc.s <<endl;/// xc.s = Hello
  return 0;
}


template parameter .. type parameter ..


Template Parameter

Type Parameter


default template parameter


Class Template Specialization


Class Template Specialization


in some cases it is possible to override the template-generated code by providing special definitions for specific types. this is called Template Specialization.
the following example defines a template class specialization for template class stream.

////TTT.h#include <iostream>using namespace std;
template<typename T>class stream{  public:    void f() {cout << "stream<T>::f()"<<endl;}};
template<>class stream<char>{  public:    void f() {cout << "stream<char>::f()"<<endl;}};
////main.cpp#include "TTT.h"int main(){  stream<int>  si;  stream<char> sc;
  si.f();////--> stream<T>::f()  sc.f();////--> stream<char>::f()
  return 0;}

In the above example, stream<char> is used as the definition of streams of chars; other streams will be handled by the template class generated from the class template.


,,,



XXX Templates VS Templates XXX


How to tell class Templates and Templates class ?

What about Template Function?





the declarations and definitions of the class template

Q_0

 does the compiler compile the class(function) template in different way?

Q_1

 How does the compiler do in concreted details if two different exists?

Question here goes.




Why can’t I separate the definition of my templates class from its declaration and put it inside a .cpp file?


If all you want to know is how to fix this situation, read the next two FAQs. But in order to understand why things are the way they are, first accept these facts:
  1. A template is not a class or a function. A template is a “pattern” that the compiler uses to generate a family of classes or functions.
  2. In order for the compiler to generate the code, it must see both the template definition (not just declaration) and the specific types/whatever used to “fill in” the template. For example, if you’re trying to use a Foo<int>, the compiler must see both the Foo template and the fact that you’re trying to make a specific Foo<int>.
  3. Your compiler probably doesn’t remember the details of one .cpp file while it is compiling another .cpp file. It could, but most do not and if you are reading this FAQ, it almost definitely does not. BTW this is called the “separate compilation model.”
Now based on those facts, here’s an example that shows why things are the way they are. Suppose you have a template Foo defined like this:






Monday, August 1, 2016

Events and Event Handlers in C Sharp :-)

<>
    This the following is really a great masterpiece of explanation and grasp of language
<>


For Source ClickMe



To understand event handlers, you need to understand delegates. In C#, you can think of a delegate as a pointer (or a reference) to a method. This is useful because the pointer can be passed around as a value.
The central concept of a delegate is its signature, or shape. That is (1) the return type and (2) the input arguments. For example, if we create a delegate void MyDelegate(object sender, EventArgs e), it can only point to methods which return void, and take an object and EventArgs. Kind of like a square hole and a square peg. So we say these methods have the same signature, or shape, as the delegate.
So knowing how to create a reference to a method, let's think about the purpose of events: we want to cause some code to be executed when something happens elsewhere in the system - or "handle the event". To do this, we create specific methods for the code we want to be executed. The glue between the event and the methods to be executed are the delegates. The event must internally store a "list" of pointers to the methods to call when the event is raised.* Of course, to be able to call a method, we need to know what arguments to pass to it! We use the delegate as the "contract" between the event and all the specific methods that will be called.
So the default EventHandler (and many like it) represents a specific shape of method (again, void/object-EventArgs). When you declare an event, you are saying which shape of method(EventHandler) that event will invoke, by specifying a delegate:
//This delegate can be used to point to methods
//which return void and take a string.
public delegate void MyEventHandler(string foo);

//This event can cause any method which conforms
//to MyEventHandler to be called.
public event MyEventHandler SomethingHappened;

//Here is some code I want to be executed
//when SomethingHappened fires.
void HandleSomethingHappened(string foo)
{
    //Do some stuff
}

//I am creating a delegate (pointer) to HandleSomethingHappened
//and adding it to SomethingHappened's list of "Event Handlers".
myObj.SomethingHappened += new MyEventHandler(HandleSomethingHappened);
(*This is the key to events in .NET and peels away the "magic" - an event is really, under the covers, just a list of methods of the same "shape". The list is stored where the event lives. When the event is "raised", it's really just "go through this list of methods and call each one, using these values as the parameters". Assigning an event handler is just a prettier, easier way of adding your method to this list of methods to be called).
shareedit





11
And now can anyone explain why the event is called EventHandler?? Of all the confusing naming conventions, this is the worst... – Joel in Gö Jun 11 '09 at 11:12
18
@Joel in Go the event is not called EventHandler - EventHandler is the contract the event must have with anyone who communicates with it. It's like "string MyString" - the string is declaring the type. event MyEventHandler TheEvent is declaring that anyone who interacts with this event must conform to the MyEventHandler contract. The Handler convention is because the contract primarily describes how to handle the event. – Rex M Jun 11 '09 at 13:26
9
How is the event fired? – alchemical Mar 22 '10 at 18:02
1
@LuftMensch from inside the class which owns the event, the syntax EventName(delegateArg1, delegateArg2); is valid. That invokes the event like a method, which kind of kicks off a chain, calling each handler assigned to that event with those arguments. – Rex M Mar 22 '10 at 19:00
10
@Rex M : thank you for the first coherent explanation for "MyEventHandler" that I have ever seen :) – Joel in Gö Apr 21 '10 at 12:01
  
@Rex M - What if you wanted to handle an event from a class that does not own the (static)object that owns the event? Like here: stackoverflow.com/questions/4816892/… – wulfgarpro Jan 29 '11 at 3:00
  
Great answer. I just read it and it brought me up to speed in no time. I would only add that the thing that should fire/raise/call the event is a listener. The listener is probably polling something like keyboard input / mouse input constantly, so it should launch a separate thread when it listens for a thing (otherwise it would hang). Then when that thing happens, it can call SomethingHappened(TheTriggeringString). – user420667Feb 12 '12 at 4:18
  
@user420667 thanks for the kind word. Something to keep in mind - eventing isn't only for external triggers. Sometimes it is useful for inverting normal program flow as well, which means there is not necessarily a poller on a separate thread. So the thing that initiates the event is separate from the concept of eventing itself. – Rex M Feb 13 '12 at 22:22
3
@Joel in Go: I felt the same way, until I started reading it as: public event<MyEventHandler> eventName :)– Michael Parker Mar 7 '13 at 14:49
6
Thank you for the phase: "The glue between the event and the methods to be executed are the delegates. ",this is really awesome. – zionpi May 10 '13 at 8:05
2
Just want to mention: "HandleSomethingHappened" is often called "OnSomethingHappened" – HoKy22 Dec 4 '13 at 20:42
3
important to note WHY the event keyword is required. It restricts access to types that do not own the event to += and -= only – Gusdor Jan 6 '14 at 13:34
  
do you have to use += new MyEventHandler(...)? can i write +=MyEventHandler(sting foo)? MyEventHandler is a delegate – user2975699 Apr 12 '14 at 21:54 
1
@user2975699 a delegate is an object, so you have to create a new instance of it. c# also allows a shorthand, Event += TheMethod. Here if it detects the method is compatible with the delegate type of the event, it'll implicitly create the delegate instance for you. – Rex M Apr 13 '14 at 14:13
1
@user2975699 you can also create an anonymous method in-line that will implicitly get a delegate: Event += (args) => //do something – Rex M Apr 13 '14 at 14:19 
  
Thanks for your well thought out post. It's worth mentioning the standard format: public delegate void MyEventHandler(object Sender, MyEventArgs e) where MyEventArgs derives from EventArgs. Seemsdn.microsoft.com/en-us/library/aa645739%28v=vs.71%29.aspx. This offers the benefit of following the generic system.eventhandler that's been built in since .Net 2.0 which allows you to use public event MyGenericEventHandler<MyPublisher, MyEventArgs> MyEvent. See codeproject.com/Articles/20550/…. – VoteCoffee Aug 11 '14 at 22:10
  
See msdn.microsoft.com/en-US/library/ms182178%28v=vs.80%29.aspx. It allows you to skip some of the extra work in declaring delegates, etc. – VoteCoffee Aug 11 '14 at 22:14 
  
The most wonderful explanation of anything i have seen in stackoverflow. – Rama Krshna Ila Jan 19 at 23:43
  
@RexM thanks for your answer Rex. I have one question. What do you mean by "raising an event"? How would 'raising an event' look like in c# code? – BKSpurgeon Mar 6 at 1:03
1
@BKSpurgeon from anywhere inside the class that declares the event, you would call it like a method:this.SomethingHappened(args);. If there are no subscribers though, SomethingHappened will be null, so make sure you check for null before trying to call it. – Rex M Mar 6 at 13:47