Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using C++ modules, is the any reason to separate function declarations (.hpp files) from their definitions (.cpp files)?

Tags:

I'm used to writing code without modules where the header files contain the function declarations like:

// foo.h 
class Foo
{
    void bar();
};

and the corresponding .cpp file contains the definition:

// foo.cpp
#include "foo.h"

void Foo::bar()
{
    // ...
}

To my knowledge, this is done to decrease compile time and reduce dependencies. When modules will be used, will this still apply? Would it be just as fast to have the class in a single file with the definitions the way Java and C# does it? If this is the case, will there be any need for both .hpp and .cpp files when using modules?

like image 543
Phlox Midas Avatar asked Sep 23 '16 17:09

Phlox Midas


People also ask

Why do we separate header and source files?

The need for header files results from the limitations that the compiler has for knowing about the type information for functions and or variables in other modules. The compiled program or library does not include the type information required by the compiler to bind to any objects defined in other compilation units.

What is an advantage of using separate files for classes?

It is nice to avoid an extra set of naming conventions, by making each file name consistent with a C++ namespace or class names one avoids the need to think up new names or convert from one convention to another.

Why is it good practice to put a class declaration in one file and the implementation in another?

Because in most cases, you'll want to use the class somewhere besides the file in which you implement it. If you make the whole program in one file, you don't need the separation.

Do you need a .h file?

The primary purpose of a header file is to propagate declarations to code files. Header files allow us to put declarations in one location and then import them wherever we need them. This can save a lot of typing in multi-file programs. This program prints “Hello, world!” to the console using std::cout.


2 Answers

The only reason I'm aware of, as the modules proposal currently stands, is to handle circular interface dependencies.

If a program is made up of modules and it does not separate function declarations from definitions, all module files will be module interfaces (as opposed to module implementations). If you want to compare them to header and code files, module interfaces could be seen as the header (.hpp) file, and module implementations could be seen as the code (.cpp) files.

Unfortunately, the modules proposal does not allow cyclic module interface dependencies. And since your program is now completely made up of module interfaces, you will therefore never be able to have two modules which depend on each other in any way (this may be improved by the proclaimed ownership declaration in the future, but this is currently not supported). The only way to resolve circular module interface dependencies is by separating declarations and definitions and placing the circular imports in the module implementation files, as opposed to circular module interface dependencies, circular module implementation dependencies are allowed.

The following code provides an example of a situation that is impossible to compile without separating declarations and definitions:

Foo module file

export module Foo;

import module Bar;

export namespace Test {
    class Foo {
    public:
        Bar createBar() {
            return Bar();
        }
    };
}

Bar module file

export module Bar;

import module Foo;

export namespace Test {
    class Bar {
    public:
        Foo createFoo() {
            return Foo();
        }
    };
}

This article shows an example on how this could be solved were the proclaimed ownership declaration available. In essence, it comes down to separating declarations and definitions.

In a perfect world the compiler would be able to handle this scenario, but alas, as far as I'm aware the current proposed implementation of modules does not support it.

like image 146
Qub1 Avatar answered Sep 21 '22 00:09

Qub1


There are still lots of reasons to use header files.

The ease of sharing and understanding an object api without seeing the underlying details is of enough use to keep them around. It's a good 20 ft view of an object, essentially being an outline.

If you are selling a library, you would include a header file, as well as an archive file or a shared library. This way you can keep information proprietary without compromising the IP of your product, and your customers can include a binary compiled for their target.

I don't believe this is possible without header files.

like image 29
Ethan Avatar answered Sep 20 '22 00:09

Ethan