CommandLineToArgV Unescaping Trying to implement something like CommandLineToArgV:

template&lt;typename InIter, typename OutIter&gt;
inline InIter CmdLineToArgvWUnescape(InIter begin, InIter end, OutIter target)
{
    if (std::distance(begin, end) &lt; 2 || *begin != L'"')
    {
        // ""s are required
        throw MalformedEscapedSequence();
    }
    ++begin; //Skip "
    std::size_t backslashCount = 0;
    for(; begin != end; ++begin)
    {
        switch(*begin)
        {
        case L'\\':
            backslashCount++;
            break;
        case L'"':
            if (backslashCount)
            {
                std::fill_n(target, backslashCount / 2, L'\\');
                *target++ = L'"';
                backslashCount = 0;
            }
            else
            {
                return ++begin;
            }
        default:
            if (backslashCount)
            {
                std::fill_n(target, backslashCount, L'\\');
                backslashCount = 0;
            }
            *target++ = *begin;
        }
    }
    throw MalformedEscapedSequence();
}
My one comment is add a comment on exactly what it is doing.

Even after studying I am not 100% sure what it does (I would need some unit tests to feel I understood it).

What I think you are trying to achieve:


Convert \" into "
Convert \\\" into \"