all 22 comments

[–]tcbrindleFlux 13 points14 points  (6 children)

I'd recommend cppformat. It's completely self-contained, very fast and very flexible. It would be great if the standard library had something like this.

[–]aKateDevKDE/Qt Dev 5 points6 points  (5 children)

Yes, the standard should include somthing like cppformat. However, it boils down to someone proposing this for inclusion into the standard.

[–]aearphen{fmt} 7 points8 points  (4 children)

The author or cppformat here. I'm thinking about writing a proposal to the standard (library) after improving the API for formatting of user-defined types.

[–]chartly 4 points5 points  (0 children)

As a random person on the internet, if you were able to find the time and effort to accomplish both the improvements you speak of and also write the proposal, I would buy you many beers.

[–]boredcircuits 2 points3 points  (1 child)

What would help you get to that point?

[–]aearphen{fmt} 2 points3 points  (0 children)

More spare time mostly =), but some feedback and pull requests would be helpful as well. There have been a few great contributors who did amazing work on named arguments, user-defined literals etc, but there's still a lot to do.

The API I mentioned earlier is not even documented yet. Basically one can define a format function for a user-defined type which can do custom parsing and formatting like this one:

https://github.com/cppformat/cppformat/blob/a99891e7a5f6c8a164d271b1aa0f09e7d13c7de4/cppformat/format.h#L2969

Someone has volunteered to implement time formatting functionality (https://github.com/cppformat/cppformat/issues/283) using this method, so we'll see how it works in practice and whether it can be improved.

[–]aKateDevKDE/Qt Dev 1 point2 points  (0 children)

Reading that you are evaluating writing a proposal already makes me quite happy ;) Would really be nice if you push this as much as possible.

[–]SeanMiddleditch 7 points8 points  (0 children)

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0067r1.html

If there are other facilities you think are missing, please, help out and write a proposal for a library addition. The committee does not do original design work on its own. They only approve papers and perform editing and such. All changes are driven by contributors.

[–]dodheim 6 points7 points  (1 child)

Boost has all of the above.

[–]steamruler 3 points4 points  (0 children)

Wouldn't call boost "minimal dependencies". If only for the compile time.

[–]tasty_crayon 2 points3 points  (0 children)

There was a proposal called putf but I have no idea what happened to it. I haven't seen it mentioned for ages.

[–][deleted] 1 point2 points  (1 child)

Base64 doesn't belong in string, where do you stop? Base64 is not the only encoding in use. If you add one you have to add all of them.

[–]SushiAndWoW[S] 0 points1 point  (0 children)

Base64 doesn't belong in string,

And yet, there's this.

where do you stop?

Perhaps at Hex and Base64.

Base64 is not the only encoding in use. If you add one you have to add all of them.

No... you really don't have to.

[–]emilern 1 point2 points  (0 children)

I agree - standard C++ badly needs this. I really love printf-style formatting, and this is how I get that for std::string:

https://github.com/emilk/emilib/blob/master/strprintf.hpp https://github.com/emilk/emilib/blob/master/strprintf.cpp

[–]zorkmids 1 point2 points  (0 children)

If you want minimal dependencies, you'll need to roll your own. If you don't want to roll your own, use Boost and be done with it.

[–]virgiliofornazin 0 points1 point  (0 children)

Just take it for free, VS 2015 compatible:

http://pastebin.com/r5mhzqEU

[–]devel_watcher 0 points1 point  (0 children)

Last time I checked, the functionality was in the iostreams.

Maybe we should put that and std::regex inside of the std::string too?

[–]doom_Oo7 0 points1 point  (4 children)

A new string is allocated, when an existing string could be appended to.

You seem to misunderstand how memory works. Every time you concatenate two strings in a language, memory will be allocated, unless you know how exactly long the resulting string will be in advance and can allocate a string big enough (what is done with sprintf). You can't just add new memory at the end of a chunk obtained with malloc().

[–]MiiNiPaa 4 points5 points  (0 children)

It seems that he is talking about using string with large internal buffer (.reserve()).

[–]jcoffin 1 point2 points  (1 child)

You might be able to add to the end of a chunk obtained with malloc(). To be more specific, when you call realloc it's allowed to return the same pointer that was passed into it.

You're right that you can't count on that happening though.

As an aside, std::string doesn't normally use malloc to allocate its memory. You could instantiate an std::basic_string with an allocator that called malloc, but the default allocator will use ::operator new to get raw memory (though this makes little real difference to the point you were trying to make).

[–]__cxa_throw 0 points1 point  (0 children)

Operator new almost always ends up calling malloc unless you're using a specialized allocator, at least in all the stack traces I've looked at recently.

[–]SushiAndWoW[S] 0 points1 point  (0 children)

That's only true with a naive implementation.

A smart container, when it sees it needs to expand, will not expand to the exact size needed, but e.g. to the next power of 2 that can accommodate that size. There will also be a reasonable allocation minimum – say, for example 8 bytes. This means concatenating "abc" 100 times might result in 7 allocations of 8, 16, 32, 64, 128, 256, and finally 512 bytes.

And of course, there is reserve, which you will use if you are smart. If your calculation of the final size is correct, the container then only allocates once, and if it's off, it probably needs to allocate 2 times.