Header lexy/callback/object.hpp

Callback lexy::construct

lexy/callback/object.hpp
namespace lexy
{
    template <typename T>
    constexpr callback auto construct;

    template <>
    constexpr callback auto construct<void>;
}

Construct an object of type T.

The primary template accepts arbitrary arguments and returns T(std::forward<Args>(args)…​) if that is well-formed, otherwise T{std::forward<Args>(args)…​}. The specialization for void accepts no arguments and does nothing to return void.

Example 1. Construct a point from two integers
struct point
{
    int x, y;
};

struct production
{
    static constexpr auto whitespace = dsl::ascii::space;

    static constexpr auto rule = [] {
        auto integer = dsl::integer<int>(dsl::digits<>);
        return dsl::twice(integer, dsl::sep(dsl::comma));
    }();
    static constexpr auto value = lexy::construct<point>;
};
Note
construct supports both types with constructor as well as aggregates or built-in types.

Callback lexy::new_

lexy/callback/object.hpp
namespace lexy
{
    template <typename T, typename PtrT = T*>
    constexpr callback auto new_;
}

Construct an object of type T on the heap.

It accepts arbitrary arguments and calls new T(std::forward<Args>(args)…​) if that is well-formed, otherwise new T{std::forward<Args>(args)…​}. Then returns a pointer of the specified type as the result.

Example 2. Construct a point on the heap and returns a std::unique_ptr
struct point
{
    int x, y;
};

struct production
{
    static constexpr auto whitespace = dsl::ascii::space;

    static constexpr auto rule = [] {
        auto integer = dsl::integer<int>(dsl::digits<>);
        return dsl::twice(integer, dsl::sep(dsl::comma));
    }();
    static constexpr auto value = lexy::construct<point>;
};