Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模板参数的排列顺序错误。对吗?_C++_Templates - Fatal编程技术网

C++ 模板参数的排列顺序错误。对吗?

C++ 模板参数的排列顺序错误。对吗?,c++,templates,C++,Templates,我希望在以下程序中有以下输出:21.2。但模板参数的列表是相反的(据我所知)。应该是这样吗 void print_id(int i, double d) noexcept { std::cout << i << ' ' << d << std::endl; } template <typename G> int mycode(G item, std::string & p) noexcept { p.append

我希望在以下程序中有以下输出:21.2。但模板参数的列表是相反的(据我所知)。应该是这样吗

void print_id(int i, double d) noexcept {
    std::cout << i << ' ' << d << std::endl;
}
template <typename G>
int mycode(G item, std::string & p) noexcept {
    p.append((const char*)&item, sizeof(G));
    return 1;
}

template<typename G>
const char* iterate(const char* &p) noexcept {
//  std::cout << (typeid(G)).name() << " "; It gets know that the first type is 'double', the next is 'int'
    const char* ans = p;
    p += sizeof(G);
    return ans;
}

template<typename ...T>
std::function<void(const char*)> state_f(void(*func)(T...)) {
    return [func](const char* p) {
        func(*(const T*)(iterate<T>(p))...);
    };
}

template<typename ...T>
std::string encode(T... tpl) noexcept {
    std::string s;
    int crutch[] = { mycode<T>(tpl, s)... };
    return s;
}

int main(void)
{
    auto f = state_f(print_id);
    f(encode(2, 1.2).c_str());
    return 0;
}
void print_id(int i,double d)无例外{

std::cout所示代码中的关键行:

 int crutch[] = { mycode<T>(tpl, s)... };
int crutch[]={mycode(tpl,s)…};
基本上,参数包将扩展为:

 int crutch[] = { mycode<double>(1.3, s), mycode<int>(2, s) };
int crutch[]={mycode(1.3,s),mycode(2,s)};
mycode
的实现,长话短说,将其参数附加到缓冲区


这里的问题是,C++中没有一个保证的评估顺序。函数调用可能首先被执行,并且每次运行同一个程序时它可能会很不一样。在这种情况下,您不能保证左到右的评估顺序。这不可编译。请提供一个。唯一的输出是在

print\u id
中,并且总是先打印一个整数,然后再打印一个双精度。您希望它如何先输出一个浮点?在调试过程中,您注意到了什么,这与预期的不一样?@eunucleara我更改了示例,但没有注意到。我期望相同的顺序就像“编码”一样