Skip to content

Small issues in docs #1

Closed
Closed
@c42f

Description

@c42f

Hi, interesting to set yet another take on this problem :)

I had a look at the docs out of curiosity and have a few comments (maybe I am
just expecting too much, since this library seems to be only a few hours old!)

Support for user-defined types

I wanted to see how you achieved this, but I can't see that it works as
expected. For instance, the following is a compile error:

format::Print("a string: {0}\n") << std::string("asdf");

I thought about implementing positional arguments in tinyformat, and the best
solution I came up with involved a type of variant class as follows:

class Arg
{
    public:
        virtual void print(std::ostream& out) = 0;
};

template<typename T>
class ArgT : public Arg
{
    const T& m_value;
    public:
        virtual void print(std::ostream& out)
        {
            out << m_value;
        }
};

You'd then construct a std::vector<Arg*> as you sweep through the arguments,
creating an ArgT for the Nth argument. (The could be stack-allocated
in the tinyformat case, though unfortunately not with the << based interface.)
Reordering can then be achived simply using indexing.

In the end I didn't do it because I didn't need the functionality and it would
introduce some extra complexity to tinyformat, which is alreay a lot less tiny
than I would like ;-) It also means a virtual function call per argument which
is less than ideal...

i18n in printf
posix printf does have a notation to support positional arguments for i18n,
and this is implemented in glibc even though it's not in C99. For example,
try out:

printf("%2$s  %1$s\n", "first_arg", "second_arg");

Good luck with your implementation!
~Chris

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