Automatic Predefs?

Started by
5 comments, last by Antheus 12 years, 1 month ago
When I have a header file that defines a class which uses an object:

class MyClass
{
public:
void DoSomething(OtherClass *ptr);
private:
.....
};


The compiler complains because it doesn't know what OtherClass is. Yet, as long as I don't use specific parts of OtherClass, I can just pre-def OtherClass at the top of this header:

// at the top:
class OtherClass;


My question is: Is there a way to have the compiler be okay with this, without adding pre-defs for every variable type passed to a function in the header? Perhaps some compiler option (Microsoft Visual Studio 2010)? Or maybe some type of pointer-type-hiding mechanism?

Thank you for any help.
Advertisement

The compiler complains because it doesn't know what OtherClass is. Yet, as long as I don't use specific parts of OtherClass, I can just pre-def OtherClass at the top of this header:

// at the top:
class OtherClass;


Yes, for what it's worth this is called a forward declaration.


My question is: Is there a way to have the compiler be okay with this, without adding pre-defs for every variable type passed to a function in the header?
[/quote]
One way might be to use precompiled headers and put your forward declarations in there, though precompiled headers have some gotchas of their own, so take care.


Or maybe some type of pointer-type-hiding mechanism?
[/quote]
You could use void*, but please don't, especially not for the sake of brevity.

Are forward declarations really that bad? Remember that in writing your code, you're talking to the compiler. The forward declaration is telling it the minimum amount of information necessary in order to be able to do its work. It's hard to say "xyz is a class" in a more succinct way than "class xyz;".

The other option is to change to a dynamic language. But if you want to stick with C++, I'd suggest just "getting over it".

My question is: Is there a way to have the compiler be okay with this, without adding pre-defs for every variable type passed to a function in the header? Perhaps some compiler option (Microsoft Visual Studio 2010)? Or maybe some type of pointer-type-hiding mechanism?

C++ is strong type language. So we always need explicitly type declaration or forwarding.
That's very good and safe.
If some compiler can accept the use of a type without declaration or forwarding, I will feel scary.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

It would be a great IDE feature, but probably a bad idea for the compiler (it would make typos much harder to track down). Something like this might be worth investigating (caveat: I haven't tried it myself).
C++ is strong type language. So we always need explicitly type declaration or forwarding.
That's very good and safe.
If some compiler can accept the use of a type without declaration or forwarding, I will feel scary.

The type is eventually defined, assuming it is used. A forward declaration is a programmer hint, required to make up for shortcomings in the language relating to how it is compiled and linked.

The forward declaration contains no information that isn't already somewhere on your computer. Hence why incorrect forward declarations result in errors when they don't match with what was expected.

There are performance reasons (and others) for having forward declarations, but I'd love to see a world without them.

There are performance reasons (and others) for having forward declarations, but I'd love to see a world without them.

Performance reasons that were developed for machines 30 years ago. One pass compilation was necessary back when main memory was measured in kilobytes. Modern statically typed languages live without them quite happily.
Performance reasons that were developed for machines 30 years ago.[/quote]

Idea behind forward declaration hasn't gone away. It definitely takes less resources to compile 100 lines of code instead of parsing several megabytes of expanded code.

It's one of those things that can be completely automated away. C++ and C to lesser extent definitely benefits from such optimization, it just cannot be performed reliably on existing code, so it's not done. Modules are constantly proposed but don't seem to make it into standard. They would eliminate one of biggest hurdles of C++ development, namely build times.

This topic is closed to new replies.

Advertisement