Tuesday, July 3, 2007

Factor parameter independent code out of templates

Commonality and variablity analysis….interesting word for a simple thing that we do

In non template code repliation is explicit, you can see that there's duplication between two functions or two classes. In template code replication is imlicit there;s only one copy of the template source code ..but replication happens when the template is instantiated multiple times.

Non type parameters and type parameters both can cause bloat.

There are a lot of factors involved in determining performance improvement like size of binary, locaity improvement, cimpiler optimizations, program's working set size.

The best way to find the effect is to just try it out on representative data sets and the platforms concerned.

Things to remember:
Templates generate multiple classes and multiple functions, so any template code not dependent on a template parameter causes bloat.

Bloat due to non type template parameters can often be eliminated by replacing tempalte parameters with function parameters ro class data members.

Bloat due to type aparameters can be reduced by sharing implementations for instantiation types with identical binary representations.


Monday, July 2, 2007

use member function templates t o accept all compatible types

real pointers support implicit conversions very well.  (eg. derived->base, non const -> const)

iterators into stl containers are almost always smart pointers

class top

class middle : public top

class bottom : public middle

now in templates to ge tmiddle to convert to top we need

template <typename t?

class smartptr {

public:

explicit smartptr(t* realptr);

};

smartptr<top> pt1 = smarptr<middle>(new middle); to get this to compile we will need the corresponding constructor in the smartptr definition, since compilers dont assume any relation between smartpttr<top> and smartptr<middle> classes.

 

easiest way to get around is ( but bad)

template typename<t>

class smartptr{

public:

template <typename u>

smartptr(const smartptr<U> & other);,....

}

but this allows converting top object to middle object also.

such functions are called generalized copy constructors. its not declared explicit sive type conversions among built in pointer types.

so what we do is simply use member initialization list to initialize parameters with true ptrs of the other object . then it can compile only if each naked ptr can be converted to the type to which we are trying to convert the object.

 

there are more things here that i dont really understand ...

c+= states that if a copy constructor is needed and you dont declare one it will be generated for you ( so if you only have a generalized template than it wont be used by the compiler )

things to remember:

use member function templates to generate functions that accept al lcompatible types

if you declare member templates for generalized copy construction or generalized assignemnt you will still need to declare normal copy constructor and copy assignment operator also.

Sunday, July 1, 2007

know how to access names in templatized base classes

when compilers encounter the definition of derived class they dont know what class it inherits from, so they wont find functions defined in the base class, since it doesnt know which base template it is inheriting from ( c++ does this base may actually be translates to a total template specialization that doesnt define the particular function in question)

so basically in templates inheritance stops working.
three ways around this :

prefix calls to base class functions with this-> ( why does it work ?...as in how does it work underneath?)
use a using declaration e,g,
template
class logginmsgsender:public msgsender {
public:
using msgsender::sendclear ;
void sendclearmsg(msginfo& info)
{
sendClear(info);
}
};

or you can exactly specify the function being called in the base class as in
msgsendder::sendclear(info)

its least desirable because if senclear is virtual than the effect of virtual is lost

note that all of these solutions basically promise the compiler that the definition of the function will be available, and so when it sees a call to senclearmsg and if it found that the expected function is not available, then it will still give you a compiler error.

so basically the availablity of a function call or something else is checked only when its usage is encountered.....so if i declare a class that doesnt define sendclear function but neither uses sendclearmsg function than it will compile and execute without error ? is that right ?

things to remember:
in derived class templates refer to names in base class templates via a this->prefix via using declarations or via an explicit base class qualification.