模板方法的模板-模板-参数推导失败(尽管已显式特化)



我正在尝试写一个可以通过c++扩展的小脚本解释器。为此,将函数处理程序插入调度表中。为了简化我的问题,handlertype定义如下(在实际代码中,它包含参数列表和返回类型的参数):

// Handler type
using function_type = void(int);

调度表,现在,是一个简单的无序映射,一个(有点)混乱的名字作为键来实现重载:

// Dispatch table
std::unordered_map<std::string, function_type*> fn_handlers;

方法可以直接添加到该表中,例如,像一个简单的方法,它接受两个类型为int的参数(在我的情况下,operator+ii是它的托管名称):

fn_handlers["operator+ii"] = my_add_handler;

然而,许多处理程序,特别是那些与基本数学相关的处理程序,确实接受各种参数,intdouble的所有组合都是有效的,从而产生4个方法和4个调度表项。因此,我决定使用模板来实现这些方法。举个例子,这可能基本上是这样的(再次简化):

template<class A, class B>
void my_add_handler(int /* simplified... */)
{
  // Something here A and B are needed
}

调度表然后像这样填充:

fn_handlers["operator+ii"] = my_add_handler<int,int>;
fn_handlers["operator+di"] = my_add_handler<double,int>;
fn_handlers["operator+id"] = my_add_handler<int,double>;
fn_handlers["operator+dd"] = my_add_handler<double,double>;

仍然有很多要输入,但现在还可以。无论如何,由于模板参数和方法签名(修改过的名称)之间存在明显的相关性,我尝试自动编写(参数名称修改将在handler_provider::add中完成):

handler_provider<int, int>::add<my_add_handler>("operator+");
handler_provider<double, int>::add<fn_add_handler>("operator+");
handler_provider<int, double>::add<fn_add_handler>("operator+");
handler_provider<double, double>::add<fn_add_handler>("operator+");

然后将开始的参数作为第二个类型的模板参数(这样就不必键入两次<int, int>部分)。

只是为了澄清;我知道我会像这样显式地专门化my_add_handler模板:

handler_provider<int, int>::add<my_add_handler<int,int>>("test");

但我想省略的正是这个重复(两倍的<int,int>)。

然而,我一直得到错误的最后一部分。handler_provider::add方法的定义如下(上面提到的参数名称修改被省略了,因为它不是这里的重点,并且按预期工作):

template<class... Ts>
struct handler_provider
{
  // Overload for templates, such as 'test_handler'
  template<template<class...> class F>
  static void add(const std::string name)
  {
    handler_provider<Ts...>::add<F<Ts...>>(name);
  }
  // Overload for non-template (or already specialized) handlers (aka. function pointers)
  template<function_type F>
  static void add(const std::string name)
  {
    fn_handlers[name] = F;
  }
};

第一个重载,如上所述,应该用于我上面描述的确切情况,下面的处理程序安装非模板函数和完全专门化的函数。

然而,这给了我一个错误,告诉他不能从上面所示的调用中推断出内部模板。我认为我根本没有告诉编译器去推断任何东西,我在调用(再次)中完成了模板参数的专门化:

handler_provider<int, int>::add<my_add_handler>("operator+");

外部可变模板class... Ts的参数显式命名为<int, int>,内部模板模板的简单参数命名为my_add_handler。然而,编译器似乎忽略了这一点。这是我得到的输出(gcc 5.4.0使用-std=c++14):

$ g++ -std=c++14 sci_e1.cpp -o sci
sci_e1.cpp: In function ‘int main()’:
sci_e1.cpp:45:55: error: no matching function for call to ‘handler_provider<int, int>::add(const char [5])’
  handler_provider<int, int>::add<my_add_handler>("operator+");
                                                             ^
sci_e1.cpp:17:15: note: candidate: template<template<class ...> class typedef F F> static void handler_provider<Ts>::add(std::__cxx11::string) [with F = F; Ts = {int, int}]
  static void add(const std::string name)
              ^
sci_e1.cpp:17:15: note:   template argument deduction/substitution failed:
sci_e1.cpp:24:15: note: candidate: template<void (* F)(int)> static void handler_provider<Ts>::add(std::__cxx11::string) [with void (* F)(int) = F; Ts = {int, int}]
  static void add(const std::string name)
              ^
sci_e1.cpp:24:15: note:   template argument deduction/substitution failed:
sci_e1.cpp:45:55: error: could not convert template argument ‘my_add_handler’ to ‘void (*)(int)’
  handler_provider<int, int>::add<my_add_handler>("operator+");
                                                             ^

我得到第二个错误,这完全没问题,应该不是问题,因为这个重载应该被踢出模板类型的重载解析。第一个错误让我抓狂。

Clang(3.9.0)更精确一些:

$ clang++ -std=c++14 sci_e1.cpp -o sci
sci_e1.cpp:45:3: error: no matching function for call to 'add'
  handler_provider<int, int>::add<my_add_handler>("test");
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sci_e1.cpp:17:15: note: candidate template ignored: invalid explicitly-specified argument for
      template parameter 'F'
  static void add(const std::string name)
              ^
sci_e1.cpp:24:15: note: candidate template ignored: invalid explicitly-specified argument for
      template parameter 'F'
  static void add(const std::string name)
              ^
1 error generated.

但我仍然不明白我错在哪里。我错过了什么?

谢谢,塞巴斯蒂安


为了更好的测试,这里有一个完整的例子:
#include <unordered_map>
#include <string>
#include <iostream>
// Handler type
using function_type = void(int);
// Dispatch table
std::unordered_map<std::string, function_type*> fn_handlers;
// Handler provider (to install new handlers)
template<class... Ts>
struct handler_provider
{
  // Overload for templates, such as 'test_handler'
  template<template<class...> class F>
  static void add(const std::string name)
  {
    handler_provider<Ts...>::add<F<Ts...>>(name);
  }
  // Overload for non-template (or already specialized) handlers (aka. function pointers)
  template<function_type F>
  static void add(const std::string name)
  {
    fn_handlers[name] = F;
  }
};

template<class A, class B>
void test_handler(int v)
{
  // Something here A and B are needed
}
void other_handler(int v)
{
  // A handler without specialization
}
int main()
{
  // Install handlers
  handler_provider<int, int>::add<test_handler>("testii");
  handler_provider<double, int>::add<test_handler>("testdi");
  handler_provider<bool, bool, int>::add<other_handler>("otherbbi");
  // Dispatch
  fn_handlers["testii"](5); // Sould call test_handler<int, int>
  fn_handlers["testdi"](5); // Should call test_handler<double, int>
  fn_handlers["otherbbi"](5); // Should call other_handler
}

问题如下:根据标准,[temp.arg.template]/1,

[a] template-argument模板的template-parameter应该是类模板的名称或别名模板,表示为id-expression.

因此你不能实例化模板
template<template<class...> class F>
static void add(const std::string name) {
    handler_provider<Ts...>::add<F<Ts...>>(name);
}

与函数模板test_handler

要解决这个问题,您必须将test_handler改为模板函数,即将其更改为

template<class A, class B>
struct test_handler {
    void operator()(int v) {
        // Something here A and B are needed
        std::cout << __PRETTY_FUNCTION__ << " called with v = " << v << std::endl;
    }
};

不幸的是,现在这不再是void(*)(int)类型,所以你不能将它插入unordered_map。因此,您必须将映射中的元素更改为std::function<function_type>,并将模板化函函数的add重载调整为

// Overload for templates, such as 'test_handler'
template<template<class...> class F>
static void add(const std::string name) {
    fn_handlers[name] = F<Ts...>{};
}
完整的代码现在看起来像这样:
#include <iostream>
#include <functional>
#include <string>
#include <unordered_map>
// Handler typ
using function_type = void(int);
// Dispatch table
std::unordered_map<std::string, std::function<function_type>> fn_handlers;
// Handler provider (to install new handlers)
template<class... Ts>
struct handler_provider {
    // Overload for templates, such as 'test_handler'
    template<template<class...> class F>
    static void add(const std::string name) {
        fn_handlers[name] = F<Ts...>{};
    }
    // Overload for non-template (or already specialized) handlers (aka. function pointers)
    template<function_type F>
    static void add(const std::string name) {
        fn_handlers[name] = F;
    }
};
template<class A, class B>
struct test_handler {
    void operator()(int v) {
        // Something here A and B are needed
        std::cout << __PRETTY_FUNCTION__ << " called with v = " << v << std::endl;
    }
};
void other_handler(int v) {
    // A handler without specialization
    std::cout << __PRETTY_FUNCTION__ << " called with v = " << v << std::endl;
}
int main() {
    // Install handlers
    handler_provider<int, int>::add<test_handler>("testii");
    handler_provider<double, int>::add<test_handler>("testdi");
    handler_provider<bool, bool, int>::add<other_handler>("otherbbi");
    // Dispatch
    fn_handlers["testii"](5); // Sould call test_handler<int, int>
    fn_handlers["testdi"](5); // Should call test_handler<double, int>
    fn_handlers["otherbbi"](5); // Should call other_handler
}

这正是你想要的,从这个颜色中可以看到。

如果你不想使用std::function,因为开销(在我的平台上,std::function使用32字节而不是8字节的指针),你也可以为处理程序编写自己的类型擦除结构。

最新更新