On Monday, December 18, 2017 at 6:40:36 AM UTC-2, Juha Nieminen wrote:
> Scott Lurndal wrote:
> > Limit yourself to C with classes
> > (don't use STL, don't use Exceptions, disable RTTI for performance).
>
> Templates can make the code more efficient with less code than the
> equivalent non-templated version (just compare std::sort with qsort,
> for instance).
This is the classic sample repeated many times. I think C programmers
understand well the problem and they can remove the pointer callback
in a specialized algorithm. Templates are convenient for algorithms
and they solved the "container problem" that still exist in C.
Templates also opened the door for language libraries that requires
type parametrization.
> Also, C++ has much more to offer in terms of efficiency than C,
> such as constexpr functions (and anything constexpr, for that matter).
I don't see constexpr as a feature for efficiency. They are convenient
like templates to express in code some computation/code generation that
can be done in compilation time.
Without constexpr we still can compute things and use the result having
the same efficiency. constexpr just make it more integrated.
The advantage of constexpr, templates and this integration with the
language is more visible when some change in code requires updates
in the generated code.
For instance, let's say you want a table of primes numbers. This
table is not affected by your code changes. In this case you
can create an external program and generate your table. If your
table is not affect by your code, it also means, that doesn't make
sense to generate this table in each build. This is the same for
factorial(5).
On the other hand, if you are experimenting and changing some params
that affects the computation then constexpr offers a
convenient way to update the result in a integrated way.
Templates also offers a convenient way of instantiation.
For instance, lets say you have a code generator that generates
a vector<T> for you. In this case, you would probably have some
flags to specify the functions you want to instantiate.
When you need a function that you didn't generated then you would
have to run the generator again.
With automatic template instantiation, when you use the template
function it will be automatically instantiated and this is very
convenient. The only disadvantage I see of this convenience is
that it's so simple to generate more instances that programmers
can easily forget that the code may be duplicated with small
differences. I think modern compilers do a hard job to avoid
code bloat. For instance, let's say a container of pointers
instantiated for pointer T1 and T2 etc.. The push_back and others
have the same code that only differs for T but T doesn't affect the
code. In this case the compiler can join the implementation of these
instantiated functions.
Generated code also offer to programmers an important property.
If we can broke a problem in small parts then we have a good
way to go. If the parts are not related then this make easy
to review and maintaining your code.
Sometimes it is difficult to isolate completely on part from other
but the bound between parts can have a pre-defined behavior that
can be checked or generated by the compiler. This make the programmer
free to think each part independently.
For instance,
class X{ ..}
class Y {X x; ..}
class Z { Y y; ...}
When we add some data member in X the class Z is affected. The constructor
of Z is generated again. But we can think in X in a very isolated way and
this property is very interesting and powerful.