1

I have some software in C++ which uses both regular and template functions, and I wish to organize my directories properly. To implement this, I followed the guidelines of this stack question and wrote header files (.hpp), files with regular functions (.cpp) and files with template functions (.tpp). However, my current directory is poorly organized, like so

/
-include/
-src/
--foo.cpp
--foo.hpp
--foo.tpp
--bar.cpp
--bar.hpp
--bar.tpp
--mysoft.cpp
--mysoft.hpp
-Makefile
-mysoft.o

where mysoft.hpp is my main file which contains #include "foo.cpp" and #include "bar.cpp".

I have a two questions regarding the improvement of this implementation:

  1. How would I organize my software tree if I wanted to move files to an include/ folder and not have them all in src/? Should I put both .hpp and .tpp files in include/, or keep the .tpp files in src/?

  2. How do I deal with the #include calls between source, header and template files if some files are in inc/?

Here are examples of typical files foo.cpp, foo.hpp and foo.tpp.

// foo.cpp
#ifndef FOO_CPP
#define FOO_CPP

#include <somelibrary>
#include "foo.hpp"

void foo_f() {
  // some function
}
// foo.hpp
#ifndef FOO_HPP
#define FOO_HPP

void foo_f();

template<class T> class fooclass {
  private:
    int var;
  public:
    void foo_g();
}

#include "foo.tpp"
// foo.tpp
#ifndef FOO_TPP
#define FOO_TPP

template<class T>
void fooclass<T>::foo_g() {
  // some other function
}
Ronan
  • 165
  • 9
  • 3
    For this particular kind of bikeshed, I like the [Pitchfork Layout](https://api.csswg.org/bikeshed/?force=1&url=https://raw.githubusercontent.com/vector-of-bool/pitchfork/spec/data/spec.bs), which was heavily influenced by Lakos's **Large-Scale C++ Software Design**, Chapter 4: Physical Hierarchy. – Eljay Dec 13 '19 at 14:37
  • Thanks for your answer. Reading through this link, I could not find anything about the placement of the .tpp file if I should choose the Separate Header Placement solution, and also about my other two questions. Any ideas? – Ronan Dec 13 '19 at 15:04
  • 1
    This is strongly opinion based. Since `*.tpp` files are included, I'd put them in the same location as the other `*.hpp` header files. I like my private or internal header files to be kept together with the other source files, and the public or external header files to be kept in a separate location (such as `/include`). My compiler uses a `-Iincludepath` flag to use a specific include directory (but that is not specified by the C++ standard, its a compiler implementation detail). I have the `*.o` file go into their own interim build file location. – Eljay Dec 13 '19 at 15:20
  • 2
    In over 15 years of C++ I've never seen anyone make a difference between header files and "template files". Why are you doing this? edit: found this: https://stackoverflow.com/q/44774036/1116364 – Daniel Jour Dec 13 '19 at 16:50
  • 2
    @DanielJour: With a similar amount of experience, I've never seen it before either. And having followed that link, I still can't see any good reason to do it. – Beta Dec 13 '19 at 23:58
  • @Eljay I have a not really settled or answered question over there about a related question: https://softwareengineering.stackexchange.com/questions/407056/the-case-against-path-expressions-in-include-directives BTW: it seems that, as this question here will need an opinion-based answer, Softwareengineering.Stackexchange would be better suited for this also. – Vroomfondel Apr 04 '20 at 17:24
  • @Beta may I turn your eye on this very interesting ;) question also? https://softwareengineering.stackexchange.com/questions/407056/the-case-against-path-expressions-in-include-directives – Vroomfondel Apr 04 '20 at 17:26
  • @Vroomfondel: Very interesting indeed. Please see my comment on besc's answer. – Beta Apr 12 '20 at 15:02

0 Answers0