As used in this answer, I'm looking for a C++11 compatible code for the same but the usage of std::quoted
prevents me from achieving that. Can anyone suggest an alternative solution?
-
5Write your own implementation of `quoted` (a useful function in many scenarios). – n. m. could be an AI Jul 23 '17 at 14:47
-
@n.m. That would be my last resort. Right now, I'd really like a quicker alternative solution. – Jesse Jul 23 '17 at 14:50
-
1Copy an existing implementation? e.g. [here](https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/std/iomanip#L461) and [here](https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/quoted_string.h#L49). – Barry Jul 23 '17 at 14:52
-
@Jesse: Quicker? It shouldn't take more than *five minutes* to code. – Nicol Bolas Jul 23 '17 at 14:52
-
I just did a very simple implementation of the output part of std::quoted, if you want: http://coliru.stacked-crooked.com/a/41626fb5b443cc33 - this took just a few minutes. I leave you the input part as a exercice ! – Synxis Jul 23 '17 at 15:05
-
3How many people can type [~200LOC](https://github.com/boostorg/io/blob/develop/include/boost/io/detail/quoted_manip.hpp) in five minutes? – cpplearner Jul 23 '17 at 15:18
-
Actually 5 mins is quite fast if you don't know the specifications / just discovered the function 5 mins earlier ;) (that's 2 lines every 3 seconds). – Synxis Jul 23 '17 at 15:22
-
1@cpplearner I would reformulate this question into "How many people can type ~200 lines of **correct** code in five minutes?" ;-) Personally, I'm definitely outside of that group. – Vasiliy Galkin Jul 23 '17 at 16:24
-
@Synxis: Your code does the wrong thing on `std::string`s with embedded nulls – Eric Jan 27 '18 at 01:22
-
@Eric Yes, but the changes are trivial. And putting `\0` in the middle of strings is a big WTF in itself – Synxis Jan 27 '18 at 11:23
1 Answers
I give my answer assuming that you expect to find a generic approach to handle such situations. The main question that defines the guideline for me is: "How long am I supposed to maintain this code for an older compiler version?"
- If I'm certain that it will be migrated to the newer toolset along with the rest of the code base (even though in a few years time, but it will inevitably happen), then I just copy-paste implementation from the standard headers of the next target version of my compiler and put it into
namespace std
in a separate header within my code base. Even though it's a very rude hack, it ensures that I have exactly the same code version as the one I'll get after migration. As I start using newer (in this case C++14-compatible) compiler, I will just remove my own"quoted.h"
, and that's it.
Important Caveat: Barry suggested to copy-paste gcc's implementation, and I agree as long as the gcc
is your main target compiler. If that's not the case, then I'd take the one from your compiler. I'm making this statement explicitly because I had troubles when I tried to copy gcc's std::nested_exception
into my code base and, having switched from Visual Studio 2013 to 2017, noticed several differences. Also, in the case of gcc
, pay attention to its license.
- If I'm in a situation where I'll have to maintain compatibility with this older compiler for quite a while (for instance, if my product targets multiple compiler version), then it's more preferable first of all to look if there's a similar functionality available in Boost. And there is, in most cases. So check out at Boost website. Even though it states
"Quoted" I/O Manipulators for Strings are not yet accepted into Boost as public components. Thus the header file is currently located in
you are able to use it from "boost/detail". And, I strongly believe that it's still better than writing your own version (despite the advice from Synxis), even though the latter can be quite simple.
- If you're obliged to maintain the old toolset and you cannot use Boost, well...then it's maybe indeed worth thinking of putting your own implementation in.

- 1,894
- 1
- 14
- 25