Rules for declaration/definition separation

Hello,

I recently stumbled upon some code in the Eigen linear algebra library which confused me about the rules for separation of declaration and definition, and interface and implementation. Specifically, they declare templated functions and classes in header files, but implement them in .tcc files. The .tcc files include the headers at the top, unlike what is suggested in the thread "http://www.cplusplus.com/forum/general/39618/" (include .tcc at the bottom of the header file). Either way, declaration and definition are explicitly separated, but are in the same translation unit at compile time because of the inclusions. Yet I had been under the impression that templated declarations had to be defined where they are declared, because the compiler needs to be able to copy the code into where it's needed elsewhere, filling in template parameter types as necessary; given that Eigen works just fine, clearly this is not the case. Similarly, I thought using the inline keyword for a function required the definition immediately, for the same reason of the preprocessor essentially copy-and-pasting the code where needed; I have yet to see a violation of this assumption though. I am thus thoroughly confused. So I have three questions on this topic, all the same question, but from different perspectives:

1) What are the actually enforced RULES for separation of declaration/definition and interface/implementation, i.e. what do compilers require in order to correctly compile code? Note my target compilers are going to be mostly GNU, with some PGI and Intel. I would like facts as opposed to opinions on this one.

2) What is the official STANDARD for separation, i.e. what do C++98 and C++11 state on this matter, even if these recommendations are not strictly enforced or universally followed? On a similar level, what rules do Boost or the real gurus follow (e.g. Herb Sutter) follow? Once again, I'd prefer to stick to facts, or as close to facts as possible, here.

3) Finally, and this part should get really interesting, what do YOU do? For the first two questions, I want the real, factual answers, because I know such answers are possible, but for this one, I want to hear it from the guys and gals in the trenches. I am comfortable with C++ programming, but have not yet fully cultivated my own style, so I'd love to hear a bunch of people's personal tastes and tricks, especially for such a fundamental and structural question as this.

Thanks for your help everybody,

-- Chris
What are the actually enforced RULES for separation of declaration/definition and interface/implementation, i.e. what do compilers require in order to correctly compile code?


Within any one file that the compiler is compiling, it must know the parameters of a function before any use of that function (so it must have seen either the declaration or the definition or both, somewhere in that file before the attempt to use the function).
Last edited on
Topic archived. No new replies allowed.