【问题标题】:Can I detect which tag dispatch overload is used at compile time?我可以检测在编译时使用了哪个标签调度重载吗?
【发布时间】:2017-11-02 17:11:30
【问题描述】:

假设我有一个仿函数,它使用标签调度从函数的多个实现中进行选择,如下所示:

// base class for all tags, indicating the "default" implementation
struct tag_base { };

// subclasses for tags that might select a different implementation
struct tag1 : tag_base { };
struct tag2 : tag1  { };
struct tag3 : tag2 { };

struct func
{
    void operator()(tag_base) { }
    void operator()(tag3) { }
};

因此,在这个简单的示例中,标签类型tag1tag2 将分派给func 的调用运算符,它采用默认实现tag_base。但是,tag3 会改为分派到不同的实现。我有兴趣在编译时检查,对于给定的函数类型func 和两个标签类型TU,它们是否会分派到func 的调用运算符的相同重载。

基本上,我想要这样的特征(只有伪代码,因为这种方法无法编译):

template <typename Func, typename T, typename U>
struct has_same_tag_overload
{
    enum { value =  (void (Func::*)(T)) &func::operator() ==
                    (void (Func::*)(U)) &func::operator() };
}

所以在上面的例子中,以下是正确的:

  • tag&lt;func, tag_base, tag1&gt;::value == 1
  • tag&lt;func, tag_base, tag2&gt;::value == 1
  • tag&lt;func, tag1, tag2&gt;::value == 1
  • tag&lt;func, tag1, tag3&gt;::value == 0
  • tag&lt;func, tag2, tag3&gt;::value == 0

这可能吗?增加难度,这在C++03中可以吗?

【问题讨论】:

  • @VTT 不,它没有,有两个原因:1. 你不能比较不同类型的指针是否相等,以及 2. 没有 void (func::*)(tag1) 类型的重载,所以特征不适用于继承。
  • 对不起,我把它转移到这里时打错了。 gcc 抱怨演员阵容无效;我想使用 T= tag1 和 U = tag2 来调用 trait,但是由于相应的重载取而代之的是 tag_base,所以它说强制转换是无效的。
  • @JasonR 这些函数实际上是void,还是对 SO 的简化?如果您将返回类型更改为与标记参数相同,我希望您可以做到这一点,但即使在那时我也只会知道在 C++11 中使用decltype 是如何做到的。
  • @VTT 我相信检查here 应该都可以工作,除了我用static_assert 编写它们的部分。检查重载是否存在只是其中的一部分,如果没有相关的重载,我们可能只能假设并拒绝编译。
  • @DanielH:函数不是void,这是一种简化,但在某些情况下,两个不同标签的返回类型是相同的。

标签: c++ metaprogramming c++03


【解决方案1】:

如果标签形成树状层次结构,因此可以写成

struct tag_base { using base = void; };
struct tag1 : tag_base { using base = tag_base; };
struct tag2 : tag1  { using base = tag1; };
struct tag3 : tag1  { using base = tag1; };
...
// or something like tag4: is_a<tag2> {}; if you don't like typing ...

那么我们可以写

template<class U>
struct tag_matcher
{
  template<class V, class W = std::enable_if_t<std::is_same<U,V>::value> >
  operator V();
};

template<typename F, typename T>
std::true_type match_tag_impl( decltype(F{}(tag_matcher<T>()))* );

template<typename F, typename T>
std::false_type match_tag_impl( ... );

template<typename F, typename T>
struct match_tag
{
  using type = std::conditional_t< decltype(match_tag_impl<F,T>(0))::value,
    T, typename match_tag<F, typename T::base>::type >;
};

template<typename F>
struct match_tag<F,void> { using type = void; };

template<typename F, typename T, typename U>
struct has_same_tag_overload:
  std::is_same< typename match_tag<F,T>::type, typename match_tag<F,U>::type > {};

这个想法是检查所有祖先以找到最衍生的匹配,然后检查两者是否相同。这是 c++11,但据我所知,您也可以使其适用于 c++03。

更新 C++03:

正如评论中提到的,上面定义的 tag_matcher 不能在 c++03 中工作,因为我们在那里没有默认的函数模板参数,因此使启用 sfinae 的转换运算符模板成为不可能。 也就是说,我们可以移动与标签转换构造函数相同的逻辑:

template<class U>
struct tag_matcher{};

// macro just for expository purposes
#define MATCHME(TAG) template<class T> TAG( tag_matcher<T>, std::enable_if_t<std::is_same<T,TAG>::value>* = 0 )

struct tag_base { using base = void; MATCHME(tag_base); };
struct tag1 : tag_base { using base = tag_base; MATCHME(tag1); };
// ...

【讨论】:

  • 这是个好主意。我想我也许可以完成这项工作;我没有考虑在标签类型本身中构建任何额外的元数据。我也没有看到任何会妨碍 C++03 实现的东西。谢谢!
  • 嗯,看了更多之后,我坚持将tag_matcher 转换运算符上的std::enable_if_t 翻译成C++03。直到 C++11 才支持函数的默认模板参数,因此通过 SFINAE doesn't seem to be possible 禁用转换运算符。您是否看到任何可以解决此问题的替代实现?
  • @jasonr 你是对的,错过了。您是否尝试过向标签添加等效的 tag_matcher 转换构造函数?
  • @jasonr 好的,现在测试了自己,它对我有用,请参阅编辑
【解决方案2】:

我能想到的最简单的方法会使func 类型的定义严重复杂化。我将在这里使用 C++14 特性:

#include <type_traits>

template <typename Func, typename Tag1, typename Tag2>
using has_same_tag_overload =
    std::is_same<decltype(Func::fn(Tag1{})), decltype(Func::fn(Tag2{}))>;

struct func
{
    static auto fn(tag_base) {
        return [] { std::cout << "tag_base\n"; };
    }
    static auto fn(tag3) {
        return [] { std::cout << "tag3\n"; };
    }

    template <typename Tag>
    void operator()(Tag tag)
    {
        fn(tag)();
    }
};

这个想法是你让你的func 结构在调用它之前创建要调用的函数,让每个函数都是一个不同的类型。这意味着我们可以简单地使用std::is_same比较函数类型。

这在 C++03 中是可行的,但代码变得更加混乱。 func 必须返回不同的函数对象类型。代替decltype,您可以使用sizeof 技巧(让func 中的每个函数对象类型具有不同的大小,以便您可以比较大小。

【讨论】:

  • 是的,我想到了类似的东西,但不幸的是返回 void 只是对 SO 的简化。我想你可以做这样的事情,但添加一个隐式转换到真正的返回类型。
  • @DanielH 使用这种技术处理返回类型非常容易:在 lambda 和 operator() 上添加返回类型
猜你喜欢
  • 1970-01-01
  • 2013-11-01
  • 1970-01-01
  • 2022-01-12
  • 1970-01-01
  • 2019-03-17
  • 1970-01-01
  • 1970-01-01
  • 2013-10-05
相关资源
最近更新 更多