考虑以下代码:
#include <iostream>
#include <functional>
template<typename... Args>
void testfunc(const std::function<void (float, Args..., char)>& func)
{
}
int main(int argc, char* argv[])
{
auto func = [](float, int, char) {};
auto sfunc = static_cast<std::function<void (float, int, char)>>(func);
testfunc<int>(sfunc);
return 0;
}
我明确指定类型是因为 ( https://stackoverflow.com/a/40476083 ):
When a parameter pack doesn't appear last in the parameter declaration, it is a non-deduced context. A non-deduced context means that the template arguments have to be given explicitly.
MSVC 成功编译它,而 gcc 和 clang 都拒绝代码:
source_file.cpp: In function ‘int main(int, char**)’:
source_file.cpp:14:24: error: no matching function for call to ‘testfunc(std::function<void(float, int, char)>&)’
testfunc<int>(sfunc);
^
source_file.cpp:5:6: note: candidate: template<class ... Args> void testfunc(const std::function<void(float, Args ..., char)>&)
void testfunc(const std::function<void (float, Args..., char)>& func)
^
source_file.cpp:5:6: note: template argument deduction/substitution failed:
source_file.cpp:14:24: note: mismatched types ‘char’ and ‘int’
testfunc<int>(sfunc);
^
source_file.cpp:14:24: note: ‘std::function<void(float, int, char)>’ is not derived from ‘const std::function<void(float, Args ..., char)>’
现在让我们做一个小改动——让我们从本地 func
中移除 int
参数,从而使模板参数包变为空:
#include <iostream>
#include <functional>
template<typename... Args>
void testfunc(const std::function<void (float, Args..., char)>& func)
{
}
int main(int argc, char* argv[])
{
auto func = [](float, char) {};
auto sfunc = static_cast<std::function<void (float, char)>>(func);
testfunc<>(sfunc);
return 0;
}
这一次,所有三个编译器都拒绝了不正确的代码。 使用 http://rextester.com/l/cpp_online_compiler_gcc 测试和本地 Visual Studio 安装。
问题:
- 第一种情况谁是正确的?
- 如何达到预期效果 - 即,我如何明确指定一个(可能为空的)参数包?
最佳答案
我们可以 block 扣:
template<typename... Args>
void testfunc(const block_deduction<std::function<void (float, Args..., char)>>& func)
与
template<class T>
struct tag_t{using type=T;};
template<class T>
using block_deduction=typename tag_t<T>::type;
现在 Args...
处于非推导上下文中。
您可以使用 SFINAE 并省略 char
来做更有趣的事情,然后测试 char
是否位于 Args...
的末尾,但是这似乎有点矫枉过正。
我敢打赌,当 gcc 和 clang 不同意 MSVC 时,MSVC 是不对的。但我没有深入研究标准来证实这一点。
关于c++ - 未作为最后一个参数传递时模板参数包扣除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45948058/