Hacker News new | past | comments | ask | show | jobs | submit login

You're either gonna write

    #include "thing.h"
or

    class Thing;
Either way, as a practical matter, you're saying, "There's something called Thing I'm going to use." C++ forces you to say that before use in each translation unit. It's not really "repeating" yourself to write it, since you haven't "said" it yet in this TU.



You are repeating yourself if you write

  class Thing;
in every file that uses a Thing. And as I said, that's generally fine.

The issue becomes when the declaration of Thing includes namespaces, template params, etc. Rewriting that is tedious and error prone.

Look at something like std::string. That's actually

  namespace std {
  template< class T > struct allocator;
  template<class charT> struct char_traits;
  template<> struct char_traits<char>;
  template<class CharT, class Traits = char_traits<CharT>,
           class Allocator = allocator<CharT>>
    class basic_string;
  using string    = basic_string<char>;
  }
No-one is forward declaring that.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: