header only C++ code or not?

K

kathy

I have question about write C++ code. I have seen some developer use
head only style (all class definition and implementation in .h file),
But others like to seperate the definition and implementation in h /
cpp file. Is this the style / favor only or there are some benefits to
use one vs the other?
 
A

Andrew Holme

kathy said:
I have question about write C++ code. I have seen some developer use
head only style (all class definition and implementation in .h file),
But others like to seperate the definition and implementation in h /
cpp file. Is this the style / favor only or there are some benefits to
use one vs the other?

You could give someone the header and a compiled binary without revealing
the inards of your implementation to them.
 
N

Noah Roberts

I have question about write C++ code. I have seen some developer use
head only style (all class definition and implementation in .h file),
But others like to seperate the definition and implementation in h /
cpp file. Is this the style / favor only or there are some benefits to
use one vs the other?

Putting implementations in the header can greatly increase
dependencies. While you can declare functions that use objects
through reference or pointer without having full definitions, you
can't actually *use* them. Thus you'll need to include anything used
by your interface even if you could have otherwise avoided it.
Further, anything you use that's not exposed to the outside world
through your interface will also need to be included so it can be
used.

This can greatly increase compile times and also causes unnecessary
binding. Consider changing something in one of the functions your
class implements. If it's in the header then everything that includes
it must recompile as well. If it's in a .cpp file then only that
needs to recompile.

Yet further, if you implement your definitions in a .cpp file you can
leverage functions that you define within it. You can split complex
functionality into smaller functions without polluting your
interface. This is not possible if that definition is in a header
(these functions will be exposed as well).

Worse, you'll very often need to use a class that also has some sort
of dependency on the class you're currently working on. For example,
you might have some sort of parent/child relationship where the child
is contained in the parent and keeps a reference back. Perhaps some
child operations use the parent. In these cases you simply cannot do
everything in the header.

Yet even worse, if you're using Visual Studio and have not changed the
default settings you can run into serious problems when implementing
definitions in the header. Visual studio tries to figure out if a
header has changed enough to warrant recompilation of its clients
(those translation units that include it). It's not particularly good
at this though and often falls prey to false negatives, leaving
translation units using old definitions of a function. I've run into
this problem more times than I care to count.

In my opinion there's ample reason to place functions in .cpp files
and only declarations in headers whenever possible. You may be
tempted to implement some functions "inline", which requires the
definition in the header, but modern compilers are capable of inlining
across translation units. You may be tempted to implement "trivial"
functions in a header, but again in my opinion all functions should be
"trivial", with more complex functions simply using combinations of
trivial functions to do their job.

I definitely side on the .cpp file definitions even though I sometimes
get lazy and do otherwise. You'll live longer if you do.
 
S

Stefan Ram

kathy said:
I have question about write C++ code. I have seen some developer use
head only style (all class definition and implementation in .h file),
But others like to seperate the definition and implementation in h /
cpp file. Is this the style / favor only or there are some benefits to
use one vs the other?

There must be at least a main function outside of any class
compiled into an object file for a program, but a library
might be header-only.

IIRC, in »boost« source code, one often sees that
header-only style. Presumably, because a lot of »boost« code
makes heavy use of templates, which have to be declared in
header files (on a C++ implementation with no support for
»export«).
 
P

Paul

kathy said:
I have question about write C++ code. I have seen some developer use
head only style (all class definition and implementation in .h file),
But others like to seperate the definition and implementation in h /
cpp file. Is this the style / favor only or there are some benefits to
use one vs the other?

Compiling templates has slightly different requirments than non templates,
maybe it was a template.
 
T

Thomas Boell

I have question about write C++ code. I have seen some developer use
head only style (all class definition and implementation in .h file),
But others like to seperate the definition and implementation in h /
cpp file. Is this the style / favor only or there are some benefits to
use one vs the other?

In addition to what others said, putting implementation in header files
can give the compiler better chances for optimization and inlining. If
the implementation is in a separate compilation unit, the compiler
can't inline because it doesn't know the function body.
 
B

Balog Pal

Thomas Boell said:
In addition to what others said, putting implementation in header files
can give the compiler better chances for optimization and inlining. If
the implementation is in a separate compilation unit, the compiler
can't inline because it doesn't know the function body.

Nothing enforces the classic "dumb linker" model.
 
G

Garrett Hartshaw

Nothing enforces the classic "dumb linker" model.

Newer gcc versions have the -flto option for this purpose. I don't
know about other compilers.
 
B

Bo Persson

Thomas said:
In addition to what others said, putting implementation in header
files can give the compiler better chances for optimization and
inlining. If the implementation is in a separate compilation unit,
the compiler can't inline because it doesn't know the function body.

Recent versions of most C++ compilers can do that.


Bo Persson
 
B

Bo Persson

Andrew said:
You could give someone the header and a compiled binary without
revealing the inards of your implementation to them.

The can also start coding against the interface even before there is
an implementation.


Bo Persson
 
T

Thomas Boell

Newer gcc versions have the -flto option for this purpose. I don't
know about other compilers.

Nice. I didn't know about GIMPLE and the -flto flag.

It seems to be rather new (released in May 2010 with gcc 4.5
apparently). If it's true that code using LTO is effectively compiled
twice [1], then that could be a reason why one would not want to use it.

[1] http://lwn.net/Articles/387447/
 
M

MikeP

Noah said:
Putting implementations in the header can greatly increase
dependencies.

On the flipside, putting implementations in a .cpp file can, and probably
will, increase dependencies, for one is more likely to include a bunch of
headers in the cpp file and think less about (or not at all about)
writing code that is as independent/modular as possible. Independence at
the header level is just an illusion/wishful-thinking.
 
M

MikeP

Noah said:
Worse, you'll very often need to use a class that also has some sort
of dependency on the class you're currently working on. For example,
you might have some sort of parent/child relationship where the child
is contained in the parent and keeps a reference back. Perhaps some
child operations use the parent. In these cases you simply cannot do
everything in the header.

One can adopt the style, though, that everything except the above goes in
the header. When programming as a lone developer, this is feasible, but
may be disastrous in team development where there is not good
architecture/design.
In my opinion there's ample reason to place functions in .cpp files
and only declarations in headers whenever possible.

With today's nice outlining IDEs, the "everything goes into the header
file" concept becomes more plausible. Especially if the software is
evolving from the bottom-up and the "design tool" is C++.
 
G

Garrett Hartshaw

On Thu, 07 Jul 2011 11:40:22 -0400
header
files

Newer gcc versions have the -flto option for this purpose. I don't
know about other compilers.

Nice. I didn't know about GIMPLE and the -flto flag.

It seems to be rather new (released in May 2010 with gcc 4.5
apparently). If it's true that code using LTO is effectively compiled
twice [1], then that could be a reason why one would not want to
use it.


Yeah, it is pretty new. I'm not sure about the whole compiles twice
thing, but it hasn't seemed too much slower when I've used it.
 
T

Thomas Boell

On Thu, 07 Jul 2011 11:40:22 -0400
"Thomas Boell" <[email protected]>

In addition to what others said, putting implementation in header
files
can give the compiler better chances for optimization and
inlining. If
the implementation is in a separate compilation unit, the compiler
can't inline because it doesn't know the function body.


Nothing enforces the classic "dumb linker" model.

Newer gcc versions have the -flto option for this purpose. I don't
know about other compilers.

Nice. I didn't know about GIMPLE and the -flto flag.

It seems to be rather new (released in May 2010 with gcc 4.5
apparently). If it's true that code using LTO is effectively compiled
twice [1], then that could be a reason why one would not want to
use it.


Yeah, it is pretty new. I'm not sure about the whole compiles twice
thing, but it hasn't seemed too much slower when I've used it.

The documentation says it also doesn't play well together with debug
symbols (yet). Doesn't matter if you turn off optimizations when
debugging of course.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top