简体   繁体   English

具有可变参数模板参数的成员函数的显式特化

[英]Explicit specialization of member function with variadic template arguments

I am writing a templated Event Handler that allows the user to send events (in the form of structs) to connected listeners. 我正在编写一个模板化的事件处理程序,允许用户将事件(以结构的形式)发送给连接的侦听器。

I have an "emit" member function that will construct an Event object from the provided variadic template parameters and forward the event. 我有一个“emit”成员函数,它将从提供的可变参数模板参数构造一个Event对象并转发该事件。 However, I would like to provide a specialised "emit" function that will detect if the provided argument is a pre-constructed Event object, in which case I could forward the event without making a superfluous copy. 但是,我想提供一个专门的“emit”函数,它将检测提供的参数是否是一个预先构造的Event对象,在这种情况下,我可以转发事件而不需要制作多余的副本。

My initial attempt... 我最初的尝试......

template <typename T_Event>
class EventHandler
{
public:

  template <typename... T_Args>
  void emit(T_Args&&... args)
  {
    printf("variadic\n");
    deliver(T_Event {std::forward<T_Args>(args)...});
  }

  void emit(const T_Event& event)
  {
    printf("reference\n");
    deliver(event);
  }

  ...
};

I used the following logic to try out both of the emit functions, but it turns out the variadic template function always takes precedence over the const reference function. 我使用以下逻辑来尝试两个emit函数,但事实证明,variadic模板函数总是优先于const引用函数。

struct Event { int x; };

EventHandler<Event> handler;
Event event {1};
handler.emit(event);
handler.emit(2);

After some more research I managed to acheive my goal by defining two versions of the variadic template function and using enable_if to execute the correct one. 经过一些研究后,我设法通过定义两个版本的可变参数模板函数并使用enable_if来执行正确的模型来实现我的目标。

  template <typename... T_Args>
  void emit(T_Args&&... args)
  {
    printf("variadic\n");
    T_Event event {std::forward<T_Args>(args)...};
    deliver(event);
  }

  template <typename... T_Args, typename = std::enable_if<std::is_same<const T_Event&, T_Args...>::value>>
  void emit(T_Args&&... args)
  {
    printf("reference\n");
    deliver(std::forward<T_Args>(args)...);
  }

This solution worked exactly as I need when I compile with GCC, but if I compile with CLANG I get the following error message: 当我使用GCC编译时,此解决方案完全按照我的需要工作,但如果我使用CLANG编译,则会收到以下错误消息:

  call to member function 'emit' is ambiguous
handler.emit(event);

  candidate function [with T_Args = <EventB &>]
void emit(T_Args&&... args)

  candidate function [with T_Args = <EventB &>, $1 =
  std::__1::enable_if<false, void>]
void emit(T_Args&&... args)

I assume that I am close to the correct solution, can anyone explain what I am doing wrong? 我认为我接近正确的解决方案,任何人都可以解释我做错了什么吗?

The trick is to force the variadic template to fail overload resolution for the parameter pack that consists of a single (optional reference to an optional const-qualified) parameter. 诀窍是强制可变参数模板对参数包的重载解析失败,该参数包由单个(可选的const限定的参数)参数组成。

This is done by dropping references and const-qualifiers from each type in the parameter pack, assembling a tuple out of them, and using std::is_same , just like in your attempt, to compare the resulting type to std::tuple<T_Event> , and then fail overload resolution in that case by negating the type traits (it is not a std::tuple<T_Event> . 这是通过从参数包中的每个类型中删除引用和const限定符,从它们中组合一个元组,并使用std::is_same ,就像在尝试中一样,将结果类型与std::tuple<T_Event> ,然后在这种情况下通过否定类型特征失败重载解析(它不是std::tuple<T_Event>

#include <type_traits>
#include <tuple>
#include <iostream>

template <typename T_Event>
class EventHandler
{
public:

    template <typename... T_Args,
          typename=std::enable_if_t
          <std::negation<std::is_same
                 <std::tuple<std::remove_const_t
                         <std::remove_reference_t
                          <T_Args>>...>,
                          std::tuple<T_Event>>
                              >::value>>
    void emit(T_Args&&... args)
    {
        std::cout << "Variadic" << std::endl;
    }

    void emit(const T_Event& event)
    {
        std::cout << "Reference" << std::endl;
    }
};

int main()
{
    EventHandler<const char *> foo;
    const char *bar="foobar";

    foo.emit(4);
    foo.emit(4, "bar");
    foo.emit(bar);
    return 0;
}

Tested with g++ with -std=c++17. 用g ++与-std = c ++ 17进行测试。 This should be doable with C++11 and C++14 by reimplementing some of the missing type traits. 通过重新实现一些缺失的类型特征,这应该适用于C ++ 11和C ++ 14。 End result: 最终结果:

$ g++ -o t -std=c++17 t.C
$ ./t
Variadic
Variadic
Reference

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM