Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #7549 > unrolled thread
| Started by | kathy <yqin_99@yahoo.com> |
|---|---|
| First post | 2011-07-06 14:27 -0700 |
| Last post | 2011-07-07 19:16 +0200 |
| Articles | 15 — 10 participants |
Back to article view | Back to comp.lang.c++
header only C++ code or not? kathy <yqin_99@yahoo.com> - 2011-07-06 14:27 -0700
Re: header only C++ code or not? "Andrew Holme" <ah@nospam.com> - 2011-07-06 22:42 +0100
Re: header only C++ code or not? "Bo Persson" <bop@gmb.dk> - 2011-07-07 19:17 +0200
Re: header only C++ code or not? Sam <sam@email-scan.com> - 2011-07-06 17:23 -0500
Re: header only C++ code or not? Noah Roberts <roberts.noah@gmail.com> - 2011-07-06 16:14 -0700
Re: header only C++ code or not? "MikeP" <mp011011@some.org> - 2011-07-07 15:35 -0500
Re: header only C++ code or not? "MikeP" <mp011011@some.org> - 2011-07-07 15:45 -0500
Re: header only C++ code or not? "Paul" <pchristor@yahoo.co.uk> - 2011-07-07 01:24 +0100
Re: header only C++ code or not? Thomas Boell <tboell@domain.invalid> - 2011-07-07 14:24 +0200
Re: header only C++ code or not? "Balog Pal" <pasa@lib.hu> - 2011-07-07 14:57 +0200
Re: header only C++ code or not? Garrett Hartshaw <ghartshaw@gmail.com> - 2011-07-07 11:40 -0400
Re: header only C++ code or not? Thomas Boell <tboell@domain.invalid> - 2011-07-07 19:38 +0200
Re: header only C++ code or not? Garrett Hartshaw <ghartshaw@gmail.com> - 2011-07-09 09:06 -0400
Re: header only C++ code or not? Thomas Boell <tboell@domain.invalid> - 2011-07-09 16:23 +0200
Re: header only C++ code or not? "Bo Persson" <bop@gmb.dk> - 2011-07-07 19:16 +0200
| From | kathy <yqin_99@yahoo.com> |
|---|---|
| Date | 2011-07-06 14:27 -0700 |
| Subject | header only C++ code or not? |
| Message-ID | <16efe2a8-09b1-4d1d-9550-f6330de89718@j15g2000yqf.googlegroups.com> |
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?
[toc] | [next] | [standalone]
| From | "Andrew Holme" <ah@nospam.com> |
|---|---|
| Date | 2011-07-06 22:42 +0100 |
| Message-ID | <HE4Rp.25728$fQ4.24899@newsfe16.ams2> |
| In reply to | #7549 |
"kathy" <yqin_99@yahoo.com> wrote in message news:16efe2a8-09b1-4d1d-9550-f6330de89718@j15g2000yqf.googlegroups.com... >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.
[toc] | [prev] | [next] | [standalone]
| From | "Bo Persson" <bop@gmb.dk> |
|---|---|
| Date | 2011-07-07 19:17 +0200 |
| Message-ID | <97m80tFq8dU1@mid.individual.net> |
| In reply to | #7551 |
Andrew Holme wrote: > "kathy" <yqin_99@yahoo.com> wrote in message > news:16efe2a8-09b1-4d1d-9550-f6330de89718@j15g2000yqf.googlegroups.com... >> 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. The can also start coding against the interface even before there is an implementation. Bo Persson
[toc] | [prev] | [next] | [standalone]
| From | Sam <sam@email-scan.com> |
|---|---|
| Date | 2011-07-06 17:23 -0500 |
| Message-ID | <cone.1309991031.542516.4622.500@monster.email-scan.com> |
| In reply to | #7549 |
[Multipart message — attachments visible in raw view] — view raw
kathy writes: > 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? Stuffing everything into the header file, including method definitions, eliminates some duplication and manual labor. You just declare the method in the header, and you're done, versus declaring the method's signature/prototype in the header, then repeating, essentially, the same signature/prototype, together with its class qualification. The amount of repeated stuff can get quite noticable, when you have a complex class hierarchy, and parameters include templates, and such. You start with a long header file. Then, you take it all, and essentially repeat it in the translation unit itself, augmenting each method declaration with its class's name, and, finally, the method itself. The declaration, of course, must match the definition, in its signature. That's when a lot of typos often happen, resulting in additional trips to the compiler. It can become a chore. Stuffing everything into the header file eliminates most of the duplication, that's pretty much the only advantage. Sometimes you can't avoid it, like when defining templates. All of that, of course, has to go into the header file, in most cases. There are two disadvantages, mainly. One, you'll have some additional indentation, and you'll need more horizontal real-estate, as opposed to defining the class methods in a separate translation. Two, the compiler has to process the header for every translation unit that includes it. Compiling each source file takes longer, and on a typical implementation each translation unit's object module contains a copy of the methods, with the linker the spending some extra cycles eliminating the dupes, when linking. That's pretty much it.
[toc] | [prev] | [next] | [standalone]
| From | Noah Roberts <roberts.noah@gmail.com> |
|---|---|
| Date | 2011-07-06 16:14 -0700 |
| Message-ID | <716bed2c-8775-4096-ab16-de7399c0fa9e@a31g2000vbt.googlegroups.com> |
| In reply to | #7549 |
On Jul 6, 2:27 pm, kathy <yqin...@yahoo.com> wrote: > 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.
[toc] | [prev] | [next] | [standalone]
| From | "MikeP" <mp011011@some.org> |
|---|---|
| Date | 2011-07-07 15:35 -0500 |
| Message-ID | <iv55um$pqn$1@dont-email.me> |
| In reply to | #7553 |
Noah Roberts wrote: > 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.
[toc] | [prev] | [next] | [standalone]
| From | "MikeP" <mp011011@some.org> |
|---|---|
| Date | 2011-07-07 15:45 -0500 |
| Message-ID | <iv55un$pqn$2@dont-email.me> |
| In reply to | #7553 |
Noah Roberts wrote: > 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++.
[toc] | [prev] | [next] | [standalone]
| From | "Paul" <pchristor@yahoo.co.uk> |
|---|---|
| Date | 2011-07-07 01:24 +0100 |
| Message-ID | <W07Rp.40$_r1.35@newsfe06.ams2> |
| In reply to | #7549 |
"kathy" <yqin_99@yahoo.com> wrote in message news:16efe2a8-09b1-4d1d-9550-f6330de89718@j15g2000yqf.googlegroups.com... >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.
[toc] | [prev] | [next] | [standalone]
| From | Thomas Boell <tboell@domain.invalid> |
|---|---|
| Date | 2011-07-07 14:24 +0200 |
| Message-ID | <20110707142407.4e2f0d3c@arcadia> |
| In reply to | #7549 |
On Wed, 6 Jul 2011 14:27:43 -0700 (PDT) kathy <yqin_99@yahoo.com> wrote: > 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.
[toc] | [prev] | [next] | [standalone]
| From | "Balog Pal" <pasa@lib.hu> |
|---|---|
| Date | 2011-07-07 14:57 +0200 |
| Message-ID | <iv4ag9$1bku$1@news.ett.com.ua> |
| In reply to | #7573 |
"Thomas Boell" <tboell@domain.invalid> > > 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.
[toc] | [prev] | [next] | [standalone]
| From | Garrett Hartshaw <ghartshaw@gmail.com> |
|---|---|
| Date | 2011-07-07 11:40 -0400 |
| Message-ID | <almarsoft.1152109586541198850@news.eternal-september.org> |
| In reply to | #7574 |
On Thu, 7 Jul 2011 14:57:44 +0200, "Balog Pal" <pasa@lib.hu> wrote: > "Thomas Boell" <tboell@domain.invalid> > > > > 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.
[toc] | [prev] | [next] | [standalone]
| From | Thomas Boell <tboell@domain.invalid> |
|---|---|
| Date | 2011-07-07 19:38 +0200 |
| Message-ID | <20110707193818.3b345f93@arcadia> |
| In reply to | #7582 |
On Thu, 07 Jul 2011 11:40:22 -0400 Garrett Hartshaw <ghartshaw@gmail.com> wrote: > On Thu, 7 Jul 2011 14:57:44 +0200, "Balog Pal" <pasa@lib.hu> wrote: > > "Thomas Boell" <tboell@domain.invalid> > > > > > > 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. [1] http://lwn.net/Articles/387447/
[toc] | [prev] | [next] | [standalone]
| From | Garrett Hartshaw <ghartshaw@gmail.com> |
|---|---|
| Date | 2011-07-09 09:06 -0400 |
| Message-ID | <almarsoft.6591275520505106110@news.eternal-september.org> |
| In reply to | #7598 |
On Thu, 7 Jul 2011 19:38:18 +0200, Thomas Boell <tboell@domain.invalid> wrote: > On Thu, 07 Jul 2011 11:40:22 -0400 > Garrett Hartshaw <ghartshaw@gmail.com> wrote: > > On Thu, 7 Jul 2011 14:57:44 +0200, "Balog Pal" <pasa@lib.hu> wrote: > > > "Thomas Boell" <tboell@domain.invalid> > > > > > > > > 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. > [1] http://lwn.net/Articles/387447/ 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.
[toc] | [prev] | [next] | [standalone]
| From | Thomas Boell <tboell@domain.invalid> |
|---|---|
| Date | 2011-07-09 16:23 +0200 |
| Message-ID | <20110709162332.176f4fa8@arcadia> |
| In reply to | #7681 |
On Sat, 09 Jul 2011 09:06:11 -0400 Garrett Hartshaw <ghartshaw@gmail.com> wrote: > On Thu, 7 Jul 2011 19:38:18 +0200, Thomas Boell > <tboell@domain.invalid> wrote: > > On Thu, 07 Jul 2011 11:40:22 -0400 > > Garrett Hartshaw <ghartshaw@gmail.com> wrote: > > > > > On Thu, 7 Jul 2011 14:57:44 +0200, "Balog Pal" <pasa@lib.hu> > wrote: > > > > "Thomas Boell" <tboell@domain.invalid> > > > > > > > > > > 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. > > > > [1] http://lwn.net/Articles/387447/ > > 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.
[toc] | [prev] | [next] | [standalone]
| From | "Bo Persson" <bop@gmb.dk> |
|---|---|
| Date | 2011-07-07 19:16 +0200 |
| Message-ID | <97m7unFppkU1@mid.individual.net> |
| In reply to | #7573 |
Thomas Boell wrote: > On Wed, 6 Jul 2011 14:27:43 -0700 (PDT) > kathy <yqin_99@yahoo.com> wrote: > >> 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. Recent versions of most C++ compilers can do that. Bo Persson
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web