解析缓冲区以创建元组时出错

Error when parsing buffer to create a tuple

我正在尝试通过读取缓冲区来构建元组。这是我的代码:

template<class T>
T read_from_stream(char *& stream)
{
    T value;
    memcpy(&value, stream, sizeof(T));
    stream += sizeof(T);
    return value;
}


template <typename ... Args>
tuple<Args...> parse(char * buffer)
{
    return tuple<Args...>{read_from_stream<Args>(buffer)...};
}

我喜欢用它

auto tup = parse<float, int, char>(buf);

现在假设缓冲区中添加的数据顺序为 float、int、char... read_from_stream 以相反的顺序被调用,即首先调用 char,然后调用 int,然后调用 float。我必须以相反的顺序(解析)指定函数模板参数类型才能正确读取数据。我希望保留订单。 我做错了什么?

顺便说下我用的是vs2013(update 4)

实际上,这看起来像是编译器中的错误。该标准要求在列表初始化期间,严格按照它们出现的顺序评估初始化列表的元素。引用 C++11 8.5.4/4:

Within the initializer-list of a braced-init-list, the initializer-clauses, including any that result from pack expansions (14.5.3), are evaluated in the order in which they appear. That is, every value computation and side effect associated with a given initializer-clause is sequenced before every value computation and side effect associated with any initializer-clause that follows it in the comma-separated list of the initializer-list. [ Note: This evaluation ordering holds regardless of the semantics of the initialization; for example, it applies when the elements of the initializer-list are interpreted as arguments of a constructor call, even though ordinarily there are no sequencing constraints on the arguments of a call. —end note ]

(强调我的)

事实上,使用 GCC,您的代码 works just fine