Skip to content

Support Incomplete Types, somehow #3180

@ingo-loehken

Description

@ingo-loehken

Hi,

I'd like to come upwith a requirement to support the combination of fmt with incomplete types, that have been specialized in formatter.

To clarify the use case, an example might help

class IncompleteType;

namespace FmtHelper
{
  fmt::appender FormatTo(fmt::appender out, IncompleteType const&); // resides in linked code
};

namespace fmt
{
   template <typename charT> 
   struct formatter<IncompleteType, charT, void>
   {
        template <typename ctxT>
        constexpr auto parse(ctxT& ctx) -> decltype(ctx.begin())
        {
            return ctx.begin();
        }

        template <typename ctxT>
        constexpr auto format(IncompleteTypeconst& x, ctxT& ctx) noexcept
        {
            return FmtHelper::FormatTo(ctx.out(), x);
        }
   };
};

Rationale

  • IncompleteType injects evil headers (i.e. WinApi or others).
  • IncompleteType is from a 3rdparty lib
  • IncompleteType should be usable in headers in combination with fmt, without the need to have full type knowledge, thus usage is not in implementation files only.

Limitations

  • it is ok, to have only support for the standard format_context and buffer, therefore fmt::appender is enough and other overloads may be added, if requirements arise.

Currently

  • it is not possible to do it in that way, because type reflection vi is_base_of for compile time strings and sophisticated noexcept specifier reflection dont allo to use incomplete types.
  • im am fully aware, that this is not a common use case and the requirement comes more from productive environments with extremly huge and divers code bases and tech stacks, including microsoft specific extensions like c+/cli and others.

Workaround

  • we currently wrap such types in another type, whcih is fully known at compile time and redirects to the implementation, but this requires client code to wrap such arguments and thats not that "nice".

godbolt: simulation

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions