C++ header only style

Started by
24 comments, last by tbrick 12 years, 4 months ago

why not just switch to a language like C# then? Seems like a lot of work just to do something that is already common place in another almost identical language.


In what universe is C# "almost identical" to C++?
Advertisement

In what universe is C# "almost identical" to C++?

Proponents of the many worlds interpretation would say "practically infinite", though identifying them is left as an exercise for the reader.

Try not to take every comment too literally :)
I've transitioned, over the course of a number of years, from using a lot of header-only files to using just a few. I think the arguments about scaling are helpful and I was just being kind of lazy about things by using so many header-only files.

Here's one of my remaining header-only files:


#ifndef cmw_ErrorWords_hh
#define cmw_ErrorWords_hh

#include <exception>
#include <flex_string.h>
#include <sstream>

class failure : public ::std::exception {
flex_string<char> whatStr;

public:
explicit failure (flex_string<char> const what_) : whatStr(what_)
{}

~failure () throw()
{}

char const* what () const throw()
{ return whatStr.c_str(); }

template <typename T>
failure& operator<< (T val)
{
::std::stringstream ss;
ss << val;
whatStr.append(ss.str().c_str());
return *this;
}
};


class eof : public ::std::exception {
flex_string<char> whatStr;

public:
explicit eof (flex_string<char> const what_) : whatStr(what_)
{}

~eof () throw()
{}

char const* what () const throw()
{ return whatStr.c_str(); }
};

#endif



Do you think I should add a .cc file in this case also?


Brian Wood
Ebenezer Enterprises
http://webEbenezer.net

Edit:

I've also tried writing that second class like this:


class eof : public failure {

public:
explicit eof (flex_string<char> const what_) : failure(what_)
{}

~eof () throw()
{}
};


That seems better, but two executables become a couple hundred bytes larger with that latter approach. The increase in the size of the executables happens with g++ 4.5.1 and 4,6.2.
Frankly you are doing yourself a huge disservice by adopting or even preferring this method of having only a single translation unit.
A lot of companies, at least here in Japan, request a sample of source code on a CD when you submit your résumé. I can only imagine the gawk on the manager’s face when he sees only a single gargantuan .CPP file and a huge unmaintainable set of headers. You are likely to give someone a heart attack, after which you may be charged with unintentional homicide.

Whether you like it or not, a single translation unit per header is the way to go. You can’t get a job using your method, you can’t work on a team, you can’t get anything done. How do you plan to use SVN or GIT? Everyone working on the same file at the same time?


There are a lot of benefits, especially organizationally, for working with one translation unit per header. Build times tend to decrease on larger projects, the code is manageable, multiple people can work on a project independent of each other, and you can get a job.

If you don’t like it, switch to another language. Don’t mangle C++ into a design paradigm for which it was never intended.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid


why not just switch to a language like C# then? Seems like a lot of work just to do something that is already common place in another almost identical language.


I was about to say something like this. That's one of the things I like about C#. Implementation is right in the same place as declaration, and usually it doesn't matter where they are in the file system in relation to each other as long as you're consistent with the namespaces. Compilation is also really fast, but that's probably to do with that it's not necessarily machine language that it's producing and the JIT does the rest.
Electronic Meteor - My experiences with XNA and game development
C++ is an old and broken language. Don't try to fix it, just accept it like it is :)

That said, I often do one-cpp style coding for early prototypes, where development time is much more important than compile time. At some level of complexity though it always breaks down and you have to start moving stuff out to separate files. But IMHO it's worth the trade-off in the early stages.

This topic is closed to new replies.

Advertisement