Why does std::apply fail with a generic function?

别等时光非礼了梦想. 提交于 2019-12-05 11:08:24

This is not new in C++17. Just from the signature of std::apply, there is no telling whether you want to pass add_generic<int>, add_generic<float>, add_generic<std::string>, or anything else. Knowing that requires more context (specifically: it requires knowing how std::apply is going to invoke it), but that information isn't available at the call site and so cannot be used for template argument deduction.

It's possible to work around that by passing one object, and making that one object capable of calling whichever instantiation of add_generic is needed:

std::cout << std::apply(
    [](auto first, auto second) { return add_generic(first, second); },
    std::make_tuple(2.0f,3.0f)) << '\n';
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!