c++ - 未作为最后一个参数传递时模板参数包扣除

标签 c++ c++11 templates variadic-templates

考虑以下代码:

#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 安装。

问题:

  1. 第一种情况谁是正确的?
  2. 如何达到预期效果 - 即,我如何明确指定一个(可能为空的)参数包?

最佳答案

我们可以 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/

相关文章:

c++ - 遍历堆栈

c++ - 由 procexp(MS 进程资源管理器)创建的 VC10 调试转储文件 - 只有堆栈信息没有变量值

c++ - 如何在 C++ 中使用 if else 和类型初始化?

c++ - 模板类友元函数的内部类

c++ - Java 通配符在 C++ 中的等价物是什么?

c# - 在 C++ 中使用非常 C# 的 DLL

c++ - MFC多线程程序可以并行运行吗?

c++ - 作为左值引用或右值拷贝的数据成员

c++ - 从 C++ 中与 C 灵活数组成员互操作的正确方法是什么?

c++ - 具有冲突方法的多重继承