boostorg / boostorg/units

Forward declarations for boost units

Open
#53 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
39
Forks
60
PR merge metrics
No merged PRs in 30d

Description

Hello dear boost developers,

as i am seeking to reduce the compile times of our code, i am currently trying to reduce heavy includes in header files. For this, i am forward declaring function parameters as in this example:

```
// Class A.h
class B;

class A
{
...
void foo(B);
}

```
However, i did not find a way to forward declare our type definitions, that rely on boost units such as the definition of a length unit (Length.h) required as a function parameter in the file Object.h:

```
// Length.h
#include
#include
#include

#include

using meter_unit = boost::units::si::meter_base_unit::unit_type;
using Length = boost::units::quantity;
BOOST_UNITS_STATIC_CONSTANT(Meter, meter_unit);
BOOST_UNITS_STATIC_CONSTANT(Meters, meter_unit)
```

```
// Object.h
#include // This include shall be avoided

class Position;
class Object {
...

bool isNearby(Position pos, Length distance);
}

```

What I Tried:
I tried forward declaring the template boost::units::quantity; but i struggeld to fwd-declare the meter_unit that contains both template class meter_base_unit and unit_type inside the namespace of meter_unit.

The meter_base_unit is defined inside boost units as below (simplified) and i guess the unit_type is defined inside the macro BOOST_TYPEOF_REGISTER_TYPE.
```

// Boost ... Units/Meter.hpp
namespace boost::units::si {
struct meter_base_unit : public base_unit
{
static std::string name() { return("meter"); }
static std::string symbol() { return("m"); }
};
}
}

BOOST_TYPEOF_REGISTER_TYPE(boost::units::si::meter_base_unit)
```

Is such a constellation even possible to forward declare completely?
I managed to forward declare the meter unit to some degree as shown here:
```
// MyBoostForwardDeclarations/Length_fwd.h
#include "boost/units/units_fwd.hpp"
#include

namespace boost::units
{
template class quantity;
}

using meter_unit = boost::units::si::meter_base_unit::unit_type;
using Length = boost::units::quantity;
```

However, this "forward declaration" header is still an expensive include because it need both _units_fwd.hpp_ and _meter.hpp_
So therefore, i would prefer to completely forward declare the Length_Unit without including any boost header. Are there any ideas on how i could achieve this?

I am happy for every contribution regarding my specific problem and every contribution leading to deeper understanding of template forward declarations.

Thanks in Advance

SegfaultCreator

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by comparing the forward declarations in Length_fwd.h with boost/units/units_fwd.hpp and boost/units/base_units/si/meter.hpp, then review the Length.h and Object.h examples. Determine whether Length and its meter unit can be declared without Boost headers; done means a supported approach or a documented explanation of why complete forward declaration is not possible.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
performance
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.