在混合运行时/编译时条件下调度标记

Tag dispatching on mixed runtime/compile time condition

本文关键字:条件下 调度 编译 混合 运行时      更新时间:2023-10-16

我有以下标签调度代码(参见LiveWorkSpace)

#include <iostream>
// traits types
struct A {}; struct B {}; struct C {};
// helpers
void fun_impl(bool, A) { std::cout << "An"; }
void fun_impl(bool, B) { std::cout << "Bn"; }
// would like to eliminate this
void fun_impl(bool b, C) 
{ 
  if(b) 
    fun_impl(b, A()); 
  else 
    fun_impl(b, B()); 
}
template<typename T>
void fun(bool b, T t)
{
    // T = A, B, or C
    fun_impl(b, t);        
}
int main()
{
    fun(true,  A()); // "A"
    fun(false, A()); // "A"
    fun(true,  B()); // "B"
    fun(false, B()); // "B"
    fun(true,  C()); // "A"
    fun(false, C()); // "B"
}

但是,此标记分发与函数fun密切相关,我需要维护 3 个辅助函数来为每个使用它的函数实现此标记调度。

参数推导失败:我试图将fun_impl抽象为 mixed_dispatch 函数对象的模板参数,但如果我随后fun_impl作为参数传递,则无法推断出需要 2 个重载中的哪一个。

template<typename T>
struct mixed_dispatch
{
    template<typename Fun>
    void operator()(Fun f, bool b)
    {
        return f(b, T());
    }
};
template<>
struct mixed_dispatch<C>
{
    template<typename Fun>
    void operator()(Fun f, bool b)
    {
        if (b) 
           return f(b, A());
        else
           return f(b, B());
    }
};
template<typename T>
void fun(bool b, T)
{
    // T = A, B, or C
    mixed_dispatch<T>()(fun_impl, b); // ERROR: Fun cannot be deduced   
}

问题:有没有其他方法可以将标签调度与被调用的函数分离?

我愿意接受使用 C++11 个可变参数模板/Boost.Fusion 或其他简化我当前代码的魔法的任何建议(我现在必须为每个使用此特定调度的函数维护 3 个而不是 2 个辅助函数,并且随着更复杂的调度,辅助函数的数量增长得更快)。

要选择重载函数之一,至少必须告诉编译器目标函数的参数类型。因此,我们可以将它们添加为模板类的类型参数mixed_dispatch

template < typename Tag, typename... Args >
class mixed_dispatch {
    std::function<void(Tag,Args...)> invoke;
public:
    // for overloaded functions
    mixed_dispatch( void(&f)(Tag,Args...) ) : invoke( f ) { }
    // for function objects
    template < typename F >
    mixed_dispatch( F&& f ) : invoke( std::forward<F>(f) ) { }
    void operator()( Args... args ) const {
        invoke( Tag(), args... );
    }
};

现在mixed_dispatch变成了一个包装器,可帮助您Tag对象传递给目标函数。如您所见,我们需要更改目标函数的签名(位有效)。

void fun_impl(A1, bool) { std::cout << "A1n"; }

在客户端代码中,如fun

template< typename T >
void fun( bool b, T )
{
    using dispatcher = mixed_dispatch<T,bool>;
    dispatcher d = fun_impl;
    d( b );
}

它能回答你的任何问题吗?

// traits types
struct A1 {}; struct B1 {};
struct A2 {}; struct B2 {};
template<class T1, class T2>
struct TypeSelector
{
    typedef T1 TTrue;
    typedef T2 TFalse;
};
typedef TypeSelector<A1, B1> C1;
typedef TypeSelector<A2, B2> C2;
// helpers
void fun_impl(bool, A1) { std::cout << "A1n"; }
void fun_impl(bool, B1) { std::cout << "B1n"; }
void fun_impl(bool, A2) { std::cout << "A2n"; }
void fun_impl(bool, B2) { std::cout << "B2n"; }
template<class TSel>
void fun_impl(bool b, TSel) { if(b) fun_impl(b, typename TSel::TTrue()); else fun_impl(b, typename TSel::TFalse()); }
template<typename T>
void fun(bool b, T t)
{
    // T = A, B, or C
    fun_impl(b, t);
}
int main()
{
    fun(true,  A1()); // "A1"
    fun(false, A1()); // "A1"
    fun(true,  B1()); // "B1"
    fun(false, B1()); // "B1"
    fun(true,  C1()); // "A1"
    fun(false, C1()); // "B1"
    fun(true,  C2()); // "A2"
    fun(false, C2()); // "B2"
}

或者你想简化其他"维度"中的代码?