Inlining
Function inlining
Attaching the BRONTO_INLINE()
attribute to a function indicates that calls to
the function should be replaced with the expression in the function's body.
class Container {
public:
bool empty() const { ... }
...
};
// Deprecated. Use the `empty` member function instead.
BRONTO_INLINE()
bool IsEmpty(const Container& c) {
return c.empty();
}
Requirements
For a function to be inlined, it must either
- have an empty body,
- have a single return statement (and no other statements), or
- have a single statement which is an expression.
Constructor inlining
Attaching the BRONTO_INLINE()
attribute to a delegating constructor indicates
that constructions invoking that constructor should be replaced with the
delegated-to constructor. This should feel similar to function inlining, as
constructors are very nearly functions.
class Interval {
public:
BRONTO_INLINE()
Interval(double lower, double upper)
: Interval(make(lower, upper).value()) {}
static std::expected<Interval, InvalidIntervalError> make(
double lower, double upper) {
if (lower > upper) { return std::nullopt; }
Interval i;
i.lower_ = lower;
i.upper_ = upper;
return i;
}
...
};
Requirements
For a constructor to be inlined, it must delegate to another constructor of the same type and otherwise have an empty body.
Type inlining
Attaching the BRONTO_INLINE()
to a type alias indicates that uses of the alias
should be replaced by the aliased type. This should also feel similar to
function inlining, but at the type, rather than
expression-level.
template <typename T, typename U>
using PtrPair BRONTO_INLINE() = std::pair<T*, U*>;
Requirements
For a type alias to be inlined it must not have variadic or non-type template parameters.