Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between using .ipp extension and .cpp extension files

Tags:

c++

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

First header file(like interface in Java):

template<class T>

class myClass1{

public:


    virtual int size() = 0;

};

second header file:

#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:

#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?

like image 525
14K Avatar asked Oct 02 '13 21:10

14K


People also ask

What is an .IPP file?

C++ source code file that contains inline functions for a program; wraps functions with a macro that can include or exclude the functions when running a program; used to turn off inlining functions during the debugging process, and turn them back on again for production code.

Should I use cc or CPP?

There is no difference. They're exactly the same.

What is the difference between CXX and CPP?

cxx is just an alternative to . cpp used by some compilers. . hpp is an attempt to distinguish header files where there are significant c and c++ differences.

What is the difference between header and cpp file?

h files, or header files, are used to list the publicly accessible instance variables and methods in the class declaration. .cpp files, or implementation files, are used to actually implement those methods and use those instance variables.


2 Answers

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.

like image 102
Daniel Frey Avatar answered Nov 05 '22 17:11

Daniel Frey


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::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();
}
like image 32
cepstr Avatar answered Nov 05 '22 16:11

cepstr