Post
Topic
Board Development & Technical Discussion
Re: minimalistic bitcoin client on D language?
by
hsoft
on 12/12/2010, 09:28:42 UTC
Putting your code in headers is a valid code style in C++. It allows current compilers to do a better job in optimization, and for templates (in general) it's required as they can't be exported between compilation units.

Boost is almost entirely written this way, so are quite a few other modern C++ libraries. They also prefer the functional programming approach using templates to 'simple inheritance'.

I'm only learning C++, but from what I could read, the having "templated" code in headers is a last resort. The preferred way to go is to instanciate your templates in your cpp files (so that you don't get linking errors). I have already completed the move from headers to cpp files in my cleaned repo, and it works well.

There is nothing "functional" about misusing templates. Functional programming is about passing functions around and having only "pure" (no side effects) functions.

The Bitcoin code uses templates for function that takes a "Stream" type (something that has a read() and a write() method), which is implemented by 2 or 3 classes in the code. Take it however you like, templating is the wrong way to go. The correct way to go is to have an abstract stream class and to make the stream classes subclass and implement the virtual read() and write method. Using templates is just sloppy.

Templates is for data structures (list of ), not for polymorphism (I used the wrong term earlier when I wrote "simple inheritance").