C++ Compilation Design: Safely extending a class - c++

I have a question about extending already used headers, sources and objects.
Before understanding what I mean you just have to accept that I want to use this design:
In a project of mine, I use only function declarations in a header, and for each definition I use a seperate source file, which will compile to a seperate object file.
Let's say I have a very simple class called List in directory "src".
The header could look like:
File: src/List.hpp
//[guard]
//[includes]
class List {
void add(int value);
void remove(int index);
void clear();
};
Now the three functions would have seperate files:
File: src/List/add.cpp
void List::add(int value) {
// Do something
}
imagine the other 2.
These will be compiled at a certain moment, and the header file will be used in other compiled classes.
Let's assume that another class called ABC uses the header file of List.
For each function in the class ABC, a object file is generated.
Now we want to adjust the header of List, we don't want to change a function, we only want to add a function:
File: src/List.hpp
//[guard]
//[includes]
class List {
void add(int value);
int find(int value);
void remove(int index);
void clear();
};
So another source file and object file is being generate, it's called in this example: src/List/find.cpp and src/List/find.o
Now my question, is this a legal way to use headers, sources and objects?
Would this generate problems, or isn't this possible at all?
Also, is the class called List in class ABC still the same as the newly created class called List?

Your design seems workable. However, I won't recommend it. And you didn't mention templates or standard containers.
My feeling is that
it is practically important (for efficiency reasons) to have a lot of (usually small) inline functions, notably inlined member functions (like getters, setters, etc...), often contained in their class Class { .... } definition.
hence, some of the member functions should be inline, either inside the class like
class Foo {
int _x;
Foo(int x) : _x(x) {};
~Foo() { _x=0; };
int f(int d) const { return _x + d; };
}
then all of constructor Foo::Foo(int), destructor Foo::~Foo and member function int Foo::f(int) are inlined
or after the class (usually easier for machine generated code) like
class Foo {
int _x;
inline Foo(int x);
inline ~Foo();
inline int f(int d) const;
};
Foo::Foo(int x) { _x = x; };
Foo::~Foo() { _x = 0; };
int Foo::f(int d) const { return _x+d; };
In both cases you need inlining (or perhaps Link Time Optimization e.g. gcc -flto -O for compiling & linking) for efficiency reasons.
The compiler can only inline functions when it knows their definition (their body).
then, every time you #include some class definition. you need to somehow get that inline function definition compiled. Either you put it in the same header, or that header should itself #include some other file (providing the definition of inlined functions)
In general, especially when using standard C++ library (and using standard containers, so e.g. #include <vector>) you'll get a lot of system headers (indirectly included). In practice, you don't want very small implementation file (i.e. having a few dozen lines of your source code per file is impractical).
Likewise, existing C++ framework libraries pull a lot of (indirect) headers (e.g. #include <QtGui> brings a big lot of code).
I suggest having C++ source files (either *.hh or *.cc) of a thousand lines each at least.
Look at the size of the preprocessed code, e.g. with g++ -H -C -E ... you'll be scared in practice: even when compiling a small C++ file of a few dozen lines of your source code, you'll have thousands of preprocessed source lines.
Hence my advice of thousand lines source file: any smaller file using the C++ standard library or some C++ framework library (Boost, Qt) is pulling a lot of source lines from indirectly included files.
See also this answer, why Google (with D.Novillo) tries hard to add preparsed headers to GCC, why LLVM/Clang (with C.Latner) wants modules in C and C++. And why Ocaml, Rust, Go, ... have modules...
You could also look at the GIMPLE representation generated by GCC, either using the MELT probe (MELT is a domain specific language to extend GCC, the probe is a simple graphical interface to inspect some internal representations of GCC like Gimple), or using -fdump-tree-all option to GCC (be careful: that option produces hundreds of dump files). You may also pass -ftime-report to GCC to understand a bit more where it is passing its time when compiling your C++ code.
For machine generated C++ code I recommend even more generating less files, but make them bigger. Generating thousands of small C++ files of a few dozen lines each is inefficient (making the total build time too long): the compiler will spend a lot of time parsing the same #include-d system headers again and again, and instantiating the same templated types (e.g. when using standard containers) a lot of times.
Remember that C++ permits to have several classes per source file (contrarily to Java (except inner classes)).
Also, if all of your C++ code is generated, you don't really need to generate header files (or you might generate one single big *.hh) because your generator should know which classes & functions are really used in each generated *.cc and could generate in that file only those useful declarations and inlined functions definitions.
P.S.: Notice that inline (like register) is just a (useful) hint to the compiler. It may avoid inlining a function marked inline (even implicitly, when inside class definition). It may also inline some functions not marked inline. However, a compiler needs to know the body of a function to inline it.

I believe whether a function is inlined is determined by the compilator (see question How will i know whether inline function is actually replaced at the place where it is called or not?). To inline a function (though this does not necessarily mean the function will absolutely be inlined at compilation) you should either define the function inside its class, or use the command "inline" before defining your function outside of it, in the header. For example :
inline int Foo::f(int d) const { return _x+d; };

Yes, that works just fine. It's how static libraries are implemented, because that makes it easier for the linker to not pull in things that aren't used.

Related

Can I implemented all C++ member function in header file but not intended for inline?

For C programs, I know I need .h, and .cpp to allow multiple .h to be read and compiled together but linked later.
However, for C++, I don't want to break up the class member function declaration and definition to two parts. Although it is the typical C++ way, I am trying to merge member function declaration and definition in one header file. I saw some people doing that. I would like to know if there is any problem to do this.
I know all members defined in .h class definition are then inline functions, but can they be non-inline in any way? Will compiler be controllable so that they don't have to be inline, for example, when the function is huge?
The VS2015 intellisense shows error when the 2s is in a member function defined in header file, but doesn't show that in cpp file. But it compiles. Is it showing that I am doing it wrong, or it's a bug for VS2015? I deleted the sdf, restarted the project, as long as copy the function into the .h file, it shows a read line under std::this_thread::sleep_for(2s);
see
class testtest {
void tryagain() {
using namespace std::chrono_literals;
auto twosec = 2s;
std::this_thread::sleep_for(2s);// problem is here
}
};
Is compiling faster if we separate the .h and .cpp?
My final compiler would be g++4.9.2, and I am using VS2015 to test build. The final code will run on an ARM with 1GB RAM on Linux.
I don't intend to argue about wich style is better, but hope to focus on whether it works, and what are the trade offs.
It is perfectly fine to place all implementation code in the .h file if you want, it usually depends on the situation. It's usually good practice to split up the interface and implementation even if all the code is in the .h file by doing something like this
class MyClass {
public:
void doSomethingUseful();
};
....
void MyClass::doSomethingUseful() {
// code goes here....
}
Not all functions will automatically be inlined, the compiler usually decides what to inline or not. Larger functions will likely not be inlined even if they're in the header. You can use the inline keyword to give a hint to the compiler that you'd like the function to be inlined but this is no guarantee (also as a small aside, functions marked as inline in a .cpp file will be inlined, but only when called from other functions within that same .cpp file)
Compile times will be slower with more code in the .h file so it's best to try and reduce the amount of code in them as much as possible. I guess in your case though it shouldn't be that noticeable.
I hope that is of some help! :)
You can put defines around functions to prevent them being implemented more than once
header file:-
class MyClass {
public:
void doSomethingUseful();
};
// inline functions
....
// non inline function
#ifdef MYCLASSCPP
void MyClass::doSomethingUseful() {
// code goes here....
}
#endif
Then define MYCLASSCPP in just one CPP file. (or complier parameter).

What happens if I implement a class in the header file? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Inline functions in C++
What does the compiler do if I completely implement a class in its header file? A typical example follows:
class MyException
{
public:
explicit MyException(const char* file, int line) file(file), line(line) {};
const char* getFile() const { return file };
int getLine() const { return line };
private:
const char* const file;
const int line;
};
My intention is to use the class like this: throw MyException(__FILE__, __LINE__).
I include this header file into each .cpp file. I suppose the compiler will compile the class as many times as it is defined and include the (identical) machine code into every object file it produces. Now, what will the linker do? I tried a simpler example (without all those pesky const's) and it compiled fine.
What would happen, if instead of a simple class, I implemented a three-screenful-long C function in a header file? And the final question, should I split my example into .h and .cpp files?
All methods will be inline methods. You may loose some minimal time on the overall compilation, but it's ok. As far as I know the only problem that can occur is if you have a static non-cost member variable. Then you have to assign a storage place for it (place a definition and and initial value if you want) presumably in a .cpp or else you will get linker errors about multiple definition.
I've seen header-only projects which had only the main() function in a CPP, but that was heavily templated.
Update for C++17: You can declare static non-const members as inline in your header file since C++17. This makes header-only libraries easily possible without gymnastics like static variables inside inline functions.
A class definition itself doesn't produce any code. It just shows users of the class how it is layed out, so they can generate appropriate code to manipulate it.
It's the member functions of the class that generate code. When you define a member function inside the class definition it gives the function an implicit inline declaration.
A function call can be compiled and linked in one of two ways:
(1) A single copy of the function code with a RETURN assembly instruction at the end can be placed in the image, and a CALL assembly instruction can be placed (along with param passing and return value transfer) at the call site to transfer control to this code.
or
(2) An entire copy of the function implementation can replace the entire function call at the call site.
A function declared inline is a recommendation to the compiler to do it the second way. Further an inline declaration allows the function to be defined in several translation units (so it can be placed in a shared header file). In order for the compiler have the option of implementing the second method, it needs a copy of the function implementation at compile-time. This isn't available if the function implementation is in a foreign translation unit.
It should also be noted that modern compilers do complicated things with functions declared inline. See:
http://gcc.gnu.org/onlinedocs/gcc/Inline.html
When you implement member functions inside a header file, all those functions become implicitly inline.
What does this mean and what implications does it have?
As per,
C++03 Standard ยง7.1.3/4:
It hints the compiler that substitution of function body at the point of call is preferable over the usual function call mechanism.
Even if the inline substitution is omitted, the other rules(especially w.r.t One Definition Rule) for inline are followed.
So Yes, every translation unit will have the definition of the inline function.This may result in increase in the size of your binaries.
Usually any good mainstream compiler will substitute function body at the point of call if needed, so marking functions inline merely for #1 is not really a good idea but if you want to make your intent clear to users of your class then you can do so by defining the functions within the header or explicitly marking your functions as inline.
Should I split my example into .h and .cpp files?
Yes, that is the usual compilation model that most of the projects use, wherein you separate the interface(.h) from the implementation(.cpp).The interfaces are shared with the users of your code as header files while the implementation is provided in the form of binaries.To some extent this provides a safeguard to your intellectual property.
This is known as the Separation Model.
C++ projects using templates will usually use the Inclusion Model rather than the Separation Model of usual C++ projects.

static inline free function [duplicate]

I know what it means when static function is declared in source file. I am reading some code, found that static function in header files could be invoke in other files.
Is the function defined in the header file? So that the actual code is given directly in the function, like this:
static int addTwo(int x)
{
return x + 2;
}
Then that's just a way of providing a useful function to many different C files. Each C file that includes the header will get its own definition that it can call. This of course wastes memory, and is (in my opinion) a quite ugly thing to be doing, since having executable code in a header is generally not a good idea.
Remember that #include:ing a header basically just pastes the contents of the header (and any other headers included by it) into the C file as seen by the compiler. The compiler never knows that the one particular function definition came from a header file.
UPDATE: In many cases, it's actually a good idea to do something like the above, and I realize my answer sounds very black-and-white about this which is kind of oversimplifying things a bit. For instance, code that models (or just uses) intrinsic functions can be expressed like the above, and with an explicit inline keyword even:
static inline int addTwo(int *x)
{
__add_two_superquickly(x);
}
Here, the __add_two_superquickly() function is a fictional intrinsic, and since we want the entire function to basically compile down to a single instruction, we really want it to be inlined. Still, the above is cleaner than using a macro.
The advantage over just using the intrinsic directly is of course that wrapping it in another layer of abstraction makes it possible to build the code on compilers lacking that particular intrinsic, by providing an alternate implementation and picking the right one depending on which compiler is being used.
It will effectively create a separate static function with the same name inside every cpp file it is included into. The same applies to global variables.
As others are saying, it has exactly the same meaning as a static function in the .c file itself. This is because there is no semantic difference between .c and .h files; there is only the compilation unit made up of the file actually passed to the compiler (usually named .c) with the contents of any and all files named in #include lines (usually named .h) inserted into the stream as they are seen by the preprocessor.
The convention that the C source is in a file named .c and public declarations are in files named .h is only a convention. But it is generally a good one. Under that convention, the only things that should appear in .h files are declarations so that you generally avoid having the same symbol defined more than once in a single program.
In this particular case, the static keyword makes the symbol be private to the module, so there isn't a multiple-definition conflict waiting to cause trouble. So in that one sense, it is safe to do. But in the absence of a guarantee that the function would be inlined, you take the risk that the function would be instantiated in every module that happened to #include that header file which at best is a waste of memory in the code segment.
I am not certain of what use cases would justify doing this at all in a generally available public header.
If the .h file is generated code and only included in a single .c file, then I would personally name the file something other than .h to emphasize that it isn't actually a public header at all. For example, a utility that converts a binary file into an initialized variable definition might write a file that is intended to be used via #include and could very well contain a static declaration of the variable, and possibly even static definitions of accessor or other related utility functions.
If you define the function in a header file (not simply declare it), a copy of the function will be generated in each translation unit (basically in each cpp file which includes this header).
This may increase the size of your executable, but this may be negligible if the function is small. The advantage is that the most compilers may inline the function, which may increase the code performance.
But there may be a big difference in doing this which wasn't mentioned in any answer. If your function uses a static local variable such as:
static int counter()
{
static int ctr = 0;
return ctr++;
}
Rather than:
//header
int counter();
//source
int counter()
{
static int ctr = 0;
return ctr++;
}
Then each source file including this header will have its own counter. If the function is declared inside the header, and defined in a source file, then the counter will be shared across your whole program.
So saying that the only difference will be performance and code size is wrong.
There is not semantic difference in defining in source file or header file, basically both means the same in plain C when using static keyword that, you are limiting the scope.
However, there is a problem in writing this in header file, this is because every time you include the header in a source file you'll have a copy of the function with same implementation which is much similar to have a normal function defined in header file. By adding the definition in header you are not achieving the what the static function is meant for.
Therefore, I suggest you should have your implementation only in your source file and not in header.
It is usefull in some "header-only" libraries with small inline functions. In a such case you always want to make a copy of the function so this is not a bad pattern. However, this gives you an easy way to insert separate interface and implementation parts in the single header file:
// header.h
// interface part (for user?!)
static inline float av(float a, float b);
// implementation part (for developer)
static inline float av(float a, float b)
{
return (a+b)/2.f;
}
Apple vector math library in GLK framework uses such constuction (e.g. GLKMatrix4.h).

Best practice for the placement of inline functions in C++

Which is better for short functions (e.g. getters and setters)
In the class definition header file
At the end of the header file
In the source file (in this case should I use the inline keyword or extern inline?)
You can't put inline functions in the source file (and have them used as inline) because their definition won't be available at the points the compiler needs them to inline the code.
Between the other two options, I tend to put one liners into the class definition and any others at the end of the header.
In the class definition header file
typically, unless your build times are more important (assuming that's not the case, based on the question). an exception follows
At the end of the header file
rarely. i have when dealing with nasty templates, just to clean things up. the body is usually so small that it is not distracting, and this can expose implementation details (as a substitute for documentation, but i figure some people will rail me for that). example:
void compute() {
assert(this->hasEnoughEnergyToCompute());
...computing
}
this approach can be good hygiene in some cases. i've actually used secondary files for this (a fourth option).
In the source file
this option is ideal - if it's visible in every translation where you call it. otherwise, send it back to the header.
in this case should I use the inline keyword or extern inline?
just 'inline'. in fact, your compiler probably declares methods inline implicitly. omit it altogether if all your required compilers support this.
My personal preference is to place it within the header file:
class A
{
private:
int a;
public:
const int getA() const { return a; }
void setA(int val) { a = val; }
};
Just because the getX, setX functions are so tiny, they can easily fit in one line.
In general, my get and set methods typically are one line (Only on a few occasions did they require actual non-trivial code).
If you had some non-trivial code in there, like bounds checking or some extra calculations, I would advise against sticking it in the header file.
Where your coding guidelines says to put them. There's no
absolute rule, but in general:
-- it's best to avoid inline functions until the profiler tells
you you need them, and a lot of programming guidelines
forbid them, and
-- it's usually best to avoid as much as possible in the class
definition that isn't relevant to the actual interface; most
coding guidelines suggest putting the implementation at the
end of the header, or even in a separate file included from
the header.
Having said that, if the function is so very simple that it fits
on the end of the line with the function declaration, I don't
think it hurts readability that much.

C/C++: Static function in header file, what does it mean?

I know what it means when static function is declared in source file. I am reading some code, found that static function in header files could be invoke in other files.
Is the function defined in the header file? So that the actual code is given directly in the function, like this:
static int addTwo(int x)
{
return x + 2;
}
Then that's just a way of providing a useful function to many different C files. Each C file that includes the header will get its own definition that it can call. This of course wastes memory, and is (in my opinion) a quite ugly thing to be doing, since having executable code in a header is generally not a good idea.
Remember that #include:ing a header basically just pastes the contents of the header (and any other headers included by it) into the C file as seen by the compiler. The compiler never knows that the one particular function definition came from a header file.
UPDATE: In many cases, it's actually a good idea to do something like the above, and I realize my answer sounds very black-and-white about this which is kind of oversimplifying things a bit. For instance, code that models (or just uses) intrinsic functions can be expressed like the above, and with an explicit inline keyword even:
static inline int addTwo(int *x)
{
__add_two_superquickly(x);
}
Here, the __add_two_superquickly() function is a fictional intrinsic, and since we want the entire function to basically compile down to a single instruction, we really want it to be inlined. Still, the above is cleaner than using a macro.
The advantage over just using the intrinsic directly is of course that wrapping it in another layer of abstraction makes it possible to build the code on compilers lacking that particular intrinsic, by providing an alternate implementation and picking the right one depending on which compiler is being used.
It will effectively create a separate static function with the same name inside every cpp file it is included into. The same applies to global variables.
As others are saying, it has exactly the same meaning as a static function in the .c file itself. This is because there is no semantic difference between .c and .h files; there is only the compilation unit made up of the file actually passed to the compiler (usually named .c) with the contents of any and all files named in #include lines (usually named .h) inserted into the stream as they are seen by the preprocessor.
The convention that the C source is in a file named .c and public declarations are in files named .h is only a convention. But it is generally a good one. Under that convention, the only things that should appear in .h files are declarations so that you generally avoid having the same symbol defined more than once in a single program.
In this particular case, the static keyword makes the symbol be private to the module, so there isn't a multiple-definition conflict waiting to cause trouble. So in that one sense, it is safe to do. But in the absence of a guarantee that the function would be inlined, you take the risk that the function would be instantiated in every module that happened to #include that header file which at best is a waste of memory in the code segment.
I am not certain of what use cases would justify doing this at all in a generally available public header.
If the .h file is generated code and only included in a single .c file, then I would personally name the file something other than .h to emphasize that it isn't actually a public header at all. For example, a utility that converts a binary file into an initialized variable definition might write a file that is intended to be used via #include and could very well contain a static declaration of the variable, and possibly even static definitions of accessor or other related utility functions.
If you define the function in a header file (not simply declare it), a copy of the function will be generated in each translation unit (basically in each cpp file which includes this header).
This may increase the size of your executable, but this may be negligible if the function is small. The advantage is that the most compilers may inline the function, which may increase the code performance.
But there may be a big difference in doing this which wasn't mentioned in any answer. If your function uses a static local variable such as:
static int counter()
{
static int ctr = 0;
return ctr++;
}
Rather than:
//header
int counter();
//source
int counter()
{
static int ctr = 0;
return ctr++;
}
Then each source file including this header will have its own counter. If the function is declared inside the header, and defined in a source file, then the counter will be shared across your whole program.
So saying that the only difference will be performance and code size is wrong.
There is not semantic difference in defining in source file or header file, basically both means the same in plain C when using static keyword that, you are limiting the scope.
However, there is a problem in writing this in header file, this is because every time you include the header in a source file you'll have a copy of the function with same implementation which is much similar to have a normal function defined in header file. By adding the definition in header you are not achieving the what the static function is meant for.
Therefore, I suggest you should have your implementation only in your source file and not in header.
It is usefull in some "header-only" libraries with small inline functions. In a such case you always want to make a copy of the function so this is not a bad pattern. However, this gives you an easy way to insert separate interface and implementation parts in the single header file:
// header.h
// interface part (for user?!)
static inline float av(float a, float b);
// implementation part (for developer)
static inline float av(float a, float b)
{
return (a+b)/2.f;
}
Apple vector math library in GLK framework uses such constuction (e.g. GLKMatrix4.h).