14K
14K

Reputation: 439

Difference between using .ipp extension and .cpp extension files

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?

Upvotes: 21

Views: 28328

Answers (3)

cepstr
cepstr

Reputation: 181

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();
}

Upvotes: 14

Mark Storer
Mark Storer

Reputation: 15868

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.

Upvotes: 0

Daniel Frey
Daniel Frey

Reputation: 56863

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.

Upvotes: 39

Related Questions