Hiding definition of C++ template class member functions [duplicate] - c++

This question already has answers here:
How to make closed source for the library written with template in C++
(3 answers)
Closed 6 years ago.
C++ Compiler needs to have access to the implementation of the methods in order to instantiate a template class. Hence we need to either include the definitions of a template in the header file that declares that template or define them in header files. But I want to hide the implementation (definitions) of my functions from my users due to many reasons and want to ensure that they have only access to the function declarations. Is it possible to do so while using templates in C++?

This is usually done (for eg) in boost by creating a directory hierarchy impl and detail.
impl will usually have the implementatios for the exposed API member function while inside the detail as you might have guessed, has the gritty details which you do not want to expose. This system is fairly well understood by the C++ developers.
As an example:
my_service.hpp
impl/my_service.hpp
impl/my_service.ipp ( OR )
detail/my_service.ipp
So, the idea is to split the header files and provide the users to only include your top level header file which will internally include the other files.
But nobody is stopping the users to include the impl header file as well directly. If you want to avoid that too, then you can do something on the lines of Prevent header from being included in some files, in compilation time?

Related

What exactly should I declare in a header file in C++? [duplicate]

This question already has answers here:
What should go into an .h file?
(12 answers)
Closed 2 years ago.
I have searched almost every website I could, but nobody has been able to answer my question. Basically what I ask, is what exactly should I place in a header file? I know of function declarations, but are variable declarations in a .h file also a thing? I need to know what the "tradition" or the "usual method" is of usage of header files.
EDIT : If this helps, I just recently started C++ and my objective with learning this language is an opportunity to get into the industry, mainly gamedev, and I need to know what the proper way of doing stuff is. Hence the question.
Header files conventionally contain declarations of entities that you wish to share between translation units.
That usually means class definitions, type aliases, and function declarations.
A translation unit, very roughly speaking, is "a .cpp file".
So, if you use a function in a.cpp, and you also use it in b.cpp, a declaration for it needs to be available in both translation units, and the best way to do that is to write it once in a header file, then include the header file in both .cpp files.
That's it.
While it's possible to declare variables in a header file, it's rare to do so. For starters, you'd want to put extern on it to make it not a definition (you don't want multiple definitions!). At that point, you have to ask yourself why you're not using a class.

Keeping template implementation details internal [duplicate]

This question already has answers here:
It is impossible to hide implementation of templates (for protection of intellectual property) from outside eyes. Am I right?
(2 answers)
How to hide an implementation helper template?
(7 answers)
hide template function declaration in library
(1 answer)
Why can templates only be implemented in the header file?
(17 answers)
Closed 3 years ago.
I've run into an issue trying to keep my implementation details out of reach of the user when writing templates because they reside in headers.
In a typical .cpp file, implementation details can be completely hidden by using internal linkage through inline namespaces or the static keyword. But, this doesn't do much in headers since they're not separate translation units.
What I've settled on is having an implementation namespace, and just assuming that the user won't be inclined to mess with it. But, I'd rather not give the user an option to see it at all. Is this possible?
What I currently do:
//template_stuff.hpp
namespace impl {
// stuff used in code below
}
namespace project {
// templates that use entities in ::impl
}
A question I have for the future is if modules will resolve this issue for me. I hope so, but I don't know enough details about the final design to conclude this.

Declaring Qt class in header file [duplicate]

This question already has answers here:
Should one use forward declarations instead of includes wherever possible?
(9 answers)
Closed 6 years ago.
I am reading book about using Qt5 (author Max Schlee), and I noticed that some examples have declared existing Qt class in the header file, for example like this:
class QProgressBar;
So, my question – why don't we just include the header file QProgressBar without declaring class QProgressBar; in our header file?
It's not about Qt, it's c++.
It's called forward declaration.
Basically in the .h you just say that QProgressbar is a class and do not complain about the fact that it's not defined. Then in the .cpp file you put the header, so that at compile time, the compiler will have everything well defined.
This can save compile time, as #include force the compiler to open more files and process more input.
This can also save on unnecessary recompilation. #include can force your code to be recompiled more often, due to unrelated changes in the header.
Of course on large projects you can have drawbacks.
You can find more HERE.

the benefit of writing the definition of method in implementation file (cpp file) instead of the header file? [duplicate]

This question already has answers here:
What are the advantages and disadvantages of implementing classes in header files?
(6 answers)
Closed 8 years ago.
In visual studio it is normal that the definition of the method will be in the header File.
when we need to write just the declaration in the header file, and write its definition in the implementation file (in its cpp file)? is any benefit to do that?
One big advantage is that you don't have to recompile every file that uses that header if you change the implementation of the function. In fact, that's the way C and C++ were designed to be used. As your projects get larger you'll appreciate the benefits of decoupling interface (in the header) from implementation (in the .cpp file). (However, if you're writing templates this does not apply)

Template Class Link-Time Error [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Why can templates only be implemented in the header file??
Hi,
I have a c++ project which consists of several header and source files. In order to modify the class with a template, I found that all the method definitions must be included in the header file itself, else resulting in a link-time error. What is the reason for this and how can I get over this ??
Thank You!!
This is intentional. Templates go in header files, end of story. There is no way around it. In order for the compiler to instantiate the template it must be able to see it, which means it must be in a header file. Templates are not like C# generics, where the model allows them to be hidden an essentially instantiated by the linker - templates are much more complicated than that, so they have to live in header files.
In your travels you will find mention of the export keyword, which was supposed to solve this. To cut a long story short, it didn't, no one implemented it (apart from one very brave compiler vendor) and it will soon not be part of C++ any more.