Skip to main content
Term "source" files was used in an unusual way, header files are also source files.
Source Link
Doc Brown
  • 220.5k
  • 35
  • 410
  • 625

When to ever use sourceseparate implementation files (cpp files) in modern c++?

I started programming in C++ about 6 months ago and started off like many, structuring projects in a C like way. I split .cpp (sourceimplementation) and .hpp (header) files and used virtual classes for interfaces.

As I have learned more I've realised that the majority of C++ code I've seen is making very extensive use of templates. It seems that sourcecpp files are rarely used, as they don't play well with templates. Moreover I have read that code in header files (and thus templates) are more heavily optimizable by the compiler (I guess because they can be 'seen' from everywhere). It's even to the extent that some projects I have seen have separate header files for the same templated code, splitting over definition and implementation.

My question is, disregarding compile times, is there ever a reason not to just exclusively use templates and header files for non-test code  ( II suppose also except for main.cpp)? Is this the commonly accepted modern c++ style?

When to ever use source files in modern c++?

I started programming in C++ about 6 months ago and started off like many, structuring projects in a C like way. I split .cpp (source) and .hpp (header) files and used virtual classes for interfaces.

As I have learned more I've realised that the majority of C++ code I've seen is making very extensive use of templates. It seems that source files are rarely used, as they don't play well with templates. Moreover I have read that code in header files (and thus templates) are more heavily optimizable by the compiler (I guess because they can be 'seen' from everywhere). It's even to the extent that some projects I have seen have separate header files for the same templated code, splitting over definition and implementation.

My question is, disregarding compile times, is there ever a reason not to just exclusively use templates and header files for non-test code( I suppose also except for main.cpp)? Is this the commonly accepted modern c++ style?

When to ever use separate implementation files (cpp files) in modern c++?

I started programming in C++ about 6 months ago and started off like many, structuring projects in a C like way. I split .cpp (implementation) and .hpp (header) files and used virtual classes for interfaces.

As I have learned more I've realised that the majority of C++ code I've seen is making very extensive use of templates. It seems that cpp files are rarely used, as they don't play well with templates. Moreover I have read that code in header files (and thus templates) are more heavily optimizable by the compiler (I guess because they can be 'seen' from everywhere). It's even to the extent that some projects I have seen have separate header files for the same templated code, splitting over definition and implementation.

My question is, disregarding compile times, is there ever a reason not to just exclusively use templates and header files for non-test code  (I suppose also except for main.cpp)? Is this the commonly accepted modern c++ style?

Tweeted twitter.com/StackSoftEng/status/1334920478377570304
Source Link
Daniel
  • 217
  • 3
  • 4

When to ever use source files in modern c++?

I started programming in C++ about 6 months ago and started off like many, structuring projects in a C like way. I split .cpp (source) and .hpp (header) files and used virtual classes for interfaces.

As I have learned more I've realised that the majority of C++ code I've seen is making very extensive use of templates. It seems that source files are rarely used, as they don't play well with templates. Moreover I have read that code in header files (and thus templates) are more heavily optimizable by the compiler (I guess because they can be 'seen' from everywhere). It's even to the extent that some projects I have seen have separate header files for the same templated code, splitting over definition and implementation.

My question is, disregarding compile times, is there ever a reason not to just exclusively use templates and header files for non-test code( I suppose also except for main.cpp)? Is this the commonly accepted modern c++ style?