开发者

What components are essential to a C++ project which are not found in a common library? [closed]

It&#开发者_如何学编程39;s difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

For most C++ projects Boost covers a lot of ground component wise but often used solutions and idioms require some boiler plate or ground work. What do you consider so essential that you would have it in every project and thus have it in a small "library"? Some things I thought of:

  • Singleton base-class (somebody will think he needs it, so he can do it properly)
  • ScopeGuard
  • Factory base-class
  • any_iterator

(The last two are in Loki but Loki has overlap with Boost and people are hesitant to use it.)

Edit: I might should add that I don't ask about the usual extensions of the standard library e.g. copy_if.


None of the above. In particular, definitely not a Singleton class, since the use of Singletons are typically an indication of a design flaw. In the past 15 years, I have never needed a Singleton class and all those that I have found in my travels were hacks or otherwise compromised the robustness of the system they were in.

Generally speaking, aside from a good, Standards-compliant compiler, a desire to never stop learning more about my language of choice and coding standards that don't restrict my movements, I have found that I need nothing in order to write complete systems.

Of particular note, over the past 15 years every job I've had has specifically forbidden the use of Boost. Although I use Boost in my own projects and in little tools I hack up, none of my production code uses it. I am a fan of Boost, but I haven't really missed it. And now with the C++0x support in VS2010, I miss it even less.

That said, over the years I have cooked up an #include library that I take with me wherever I go of useful little things and gizmos. It includes:

  • An exception framework
  • A version of sprintf that works with std::string
  • A high-resolution timer class which I use primarily for development, stress testing & debugging
  • An implementation of transform_if
  • An implementation of copy_if

And a few other STL extensions which I use very rarely.


My small library, that I carry along with most projects contains very practical tools:

  • assert utilities (release & debug assert with a user dialog with details and buttons for "start debugger", "ignore this assertion", "always ignore..")
  • Buffer utilities to avoid working with "plain" heap arrays (class HeapBuffer and class SharedHeapBuffer with ref counting)
  • logging facilities
  • UTF8 / UCS2 encode/decode
  • configuration utilities (class CfgValue with one-liner string-to-number/bool conversion methods)
  • some fast string to number and number to string routines
  • fast float to int conversion routines
  • explode/implode numbers/strings on separator utilities
  • ini file parser & writer
  • timer class and some quick&dirty profiling tools
  • mutexes, conditions, r/w-locks, multi-threading utilities (but from time to time I replace more and more of that with boost locks and thread utilities)
  • a lightweight messaging system "construction kit" (messages, ports, sender, handlers, sinks, dispatchers, routers, threaded sinks, threadpool sinks and so on)


None of the ones you suggested.

A singleton is a horrible antipattern, and the last thing I'd want in my C++ programs is more of them. And if you use RAII consistently for your own classes, you don't really need a separate ScopeGuard class.

A factory base-class? What exactly would it do? I don't really see enough common functionality between factories that it'd be worth putting in a single universal base class.

And I'm not really sure what you mean by an any_iterator. :)

The things that are essential to a C++ project are the ones people put in libraries. And if something is not in any of the common libraries, then it is because it is not commonly needed (or because it has to be customized for the individual project, so a library version has little value)

So your question could basically be rephrased as "what would be an obvious addition to popular libraries, which hasn't already been added to them", and the answer, just as obviously, is "nothing, because if the idea was obvious, the library writers thought of it as well, and so they already added it"


Maybe I don't write the sheer volume of code as other people (been using C++ professionally for six years), but I'm going with: nothing. Any time I've wished an idiom was in the standard library or Boost, that's a clue that maybe it isn't the best way to go. Often times you can express your concepts more simply by rewriting them to take advantage of existing constructs. Good code is easy code. Let the geniuses behind Boost take care of the complicated stuff for you.


Ring or circular buffer. This an often used data structure in Embedded Systems.


logging class (that also works in a Gui)


An encryption library or two wrapped up in a nice interface.

Hashing algorithms can also be quite useful.


  • a good logging mechanism such as Boost.Log or log4cxx
  • unit testing framework such as Boost.Test or google test
  • documentation, preferably using doxygen
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜