17

Suppose I have 2 header files, 1 .ipp extension file and a main.cpp file:

First header file myClass1.h (like interface in Java):

template<class T>
class myClass1{
    public:
        virtual int size() = 0;
};

second header file myClass2.h:

#include "myClass1.h"
template<class T>
class myClass2 : public myClass1<T>{
     public:
          virtual int size();
     private:
         int numItems;
};        
#include "myClass2.ipp"

And then is my myClass2.ipp file:

template <class T>
int myClass2<T>::size()
{    
  return numItems;
}

Last one is my main.cpp:

#include "myclass2.h"
void tester()
{
  myClass2<int> ForTesting;
  if(ForTesting.size() == 0)
  {
    //......
  } 
  else 
  {
   //.....
  }
}

int main(){
   tester();
   return 0;
}

myClass1, myClass2 and myClass2.ipp belong to header file. main.cpp in source file. What's the advantages by using this way to implement your program instead of using just .h and .cpp files? And what is .ipp extension file? The difference between .ipp and .cpp?

Uchendu
  • 1,016
  • 12
  • 20
14K
  • 399
  • 1
  • 3
  • 15
  • This may help with at least part of the question. http://stackoverflow.com/questions/5171502/c-vs-cc-vs-cpp-vs-hpp-vs-h-vs-cxx – asafreedman Oct 02 '13 at 21:36

3 Answers3

36

TR;DR

The .cpp file is a separate translation unit, the .ipp is included from the header and goes into all translation units including that header.

Explanation

Before templates, you put the declarations of methods in the header file and the implementation went to a .cpp file. These files were compiled separately as their own compilation unit.

With templates, this is no longer possible almost all template methods need to be defined in the header. To separated them at least on a logical level, some people put the declarations in the header but move all implementations of template methods to .ipp files (i for "inline") and include the .ipp file at the end of the header.

the_storyteller
  • 2,335
  • 1
  • 26
  • 37
Daniel Frey
  • 55,810
  • 13
  • 122
  • 180
  • 1
    Some sites use the suffix ".hpp" instead of ".h" for C++ header files; some use ".h++" some use ".H" and some use ".hxx". YMMV. One place I worked use the ".ipp" suffix for files which were intended to be compiled with the Intel C++ compiler. While the compiler may use the extension to distinguish between a C vs C++ compilation unit, for the most part compilers don't pay much mind to them. – kfsone Oct 02 '13 at 21:51
  • 1.put the declarations of methods in the header file and the implementation went to a .cpp file. 2.put the declarations in the header but move all implementations of template methods to .ipp files (i for "inline") and include the .ipp file at the end of the header. Why some people choose the second one? Is it better? – 14K Oct 03 '13 at 01:05
  • 1
    @14K It's not possible with templates to put the definitions into a separate compilation unit as the compiler needs to see the definition everywhere the template gets instantiated. You therefore have only two options: Put *everything* into the header *or* put the declarations into the header and the template definitions into a separate file which is included at the end of the header. – Daniel Frey Oct 03 '13 at 07:05
  • 3
    It helps to keep in mind that class templates are not classes and template member functions are not functions. They are **patterns** for **creating** classes and functions, so they have to be completely visible wherever they are used. – Pete Becker Oct 03 '13 at 13:19
  • @DanielFrey Unless you know the full set of specializations needed, and thus can use explicit instantiation. – Deduplicator Oct 10 '18 at 10:41
  • @Deduplicator True, but from my experience this is very rarely the case. – Daniel Frey Oct 10 '18 at 14:41
12

One more advantage I see in using .ipp files is that you can choose whether to include the implementation part of the templates. This allows you to reduce the compilation time by instantiating your templates for some parameters in a .cpp file, such that they are pre-compiled, while keeping the possibility to instantiate the templates for other parameters. Example:

// x.hpp
template <typename T>
struct X
{
    int f();
}
// x.ipp
#include "x.hpp"

template <typename T>
int X<T>::f()
{
    return 42;
}
// x.cpp
#include "x.ipp"

// Explicit instantiation of X<> for int and double;
// the code for X<int> and X<double> will be generated here.
template class X<int>;
template class X<double>;
// foo.cpp
// Compilation time is reduced because 
// the definitions of X member functions are not parsed.
#include "x.hpp"

void foo()
{
    X<int> x;
    x.f();
}
// bar.cpp
// Here we need to include the .ipp file because we need to instantiate
// X<> for a type which is not explicitly instantiated in x.cpp.
#include "x.ipp"
#include <string>

void bar()
{
    X<std::string> x;
    x.f();
}
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
cepstr
  • 161
  • 1
  • 3
0

It's my understanding that file extensions (.h, .cpp, etc) in C++ are CONVENTIONS. You can call them what you want, and the compiler will just do its thing. You'll note that the standard template library include files don't have extensions.

#include will pull in ANY file that its given. Or will it? Wikipedia specifically mentions "text file", so trying to include something like a .jpg might fail on that line (instead of when the compiler tries to parse whatever garbage you just handed it).

Supporting tools on the other hand? I'm guessing there are tools out there that will choke horribly if you color too far outside the lines. I haven't run into it, but I haven't tried changing my .cpp file extensions to .seapeapee or something. Sea pea pee obviously being a reference to a aquatic plant (I just made up) that occasionally releases liquids high in uric acid. Obviously.

Other languages like Java are far more stringent, requiring specific extensions such as .java in order to compile correctly.

Mark Storer
  • 15,672
  • 3
  • 42
  • 80