1

我正在寻找一种方法来提取类型std::tuple以定义方法签名。采取以下(人为的)示例:

template <typename RetT, typename... ArgsT>
class A
{
public:
    typedef RetT ReturnType;
    typedef std::tuple<ArgsT...> ArgTypes;

    RetT doSomething(ArgsT... args)
    {
        // Doesn't make much sense, but it's just an example
        return (RetT) printf(args...);
    }
};

template <typename Enable, typename RetT, typename... ArgsT>
class AAdapter;

// Simply pass arguments along as-is
template <typename RetT, typename... ArgsT>
class AAdapter<std::enable_if_t<!std::is_same_v<RetT, float>>, RetT, ArgsT...> : public A<RetT, ArgsT...> {};

// Add additional first argument if RetT is float
template <typename RetT, typename... ArgsT>
class AAdapter<std::enable_if_t<std::is_same_v<RetT, float>>, RetT, ArgsT...> : public A<RetT, const char*, ArgsT...> {};



template <typename RetT, typename... ArgsT>
class B
{
public:
    typedef AAdapter<void, RetT, ArgsT...> AAdapter;

    // This needs to have the same method signature (return type and argument types) as AAdapter::doSomething()
    template <size_t... Index>
    typename AAdapter::ReturnType doSomething (
        typename std::tuple_element<Index, typename AAdapter::ArgTypes>::type... args
    ) {
        return a.doSomething(args...);
    }

public:
    AAdapter a;
};


int main(int argc, char** argv)
{
    // I would like to be able to remove the <0,1,2> and <0,1,2,3> below.
    B<int, const char*, int, int> b1;
    b1.doSomething<0,1,2>("Two values: %d, %d\n", 1, 2);

    B<float, const char*, int, int> b2;
    b2.doSomething<0,1,2,3>("Three values: %s, %d, %d\n", "a string", 1, 2);

    return 0;
}

考虑 AAdapter 更改、添加或删除不透明参数类型的方式。基本上,我想B::doSomething()简单地重定向到B::AAdapter::doSomething(),所以我希望这两种方法都具有完全相同的签名。问题是:如何B::AAdapter::doSomething()从 inside获取参数类型B

B::doSomething()在上面的代码中的定义是我来得最远的:我正在std::tuple使用里面的参数类型进行类型定义A,所以我可以将它们解包回B. 不幸的是,通过上述方法,我仍然需要Index...在调用时手动提供模板参数B::doSomething()。当然,必须有一种方法可以Index...从元组的大小中自动推断出这些参数。我曾考虑过使用 的方法std::make_integer_sequence,但这需要我为序列本身定义一个额外的方法参数(并且它不能是具有默认值的最后一个参数,因为在参数包之后不允许使用其他参数)。

有没有什么办法可以做到这一点,不管有没有 std::tuple?需要 C++17 的解决方案会很好。

编辑1:

我现在意识到,我可以通过B 继承AAdapter不是将AAdapter对象作为成员来规避特定应用程序中的问题,但我仍然想知道如何在不必这样做的情况下解决问题。

编辑2:

也许一些关于为什么AAdapter存在以及我想要实现的附加信息。我正在围绕现有的 C API 实现一种包装类,实际上需要在另一个进程中调用,RPC 样式。因此,如果用户想在远程进程中调用 C 函数,他们将改为在本地调用我的包装类中的相应方法,该方法处理所有 RPC 内容,如类型转换、实际远程调用和其他丑陋的细节。这个包装类B在我上面的代码中表示为。现在我的包装方法签名通常不会具有与 C 函数完全相同的签名。例如,包装器可能具有而不是C 函数具有std::string_view的一对。const char*, size_t由于这里不重要的原因,它还需要有一个输出参数(一个指针),而 C 函数有时有一个返回值。

为了让我不必定义两个单独的方法签名(实际上是三个)并编写代码来转换每个单独的参数,我只将其中一个签名作为模板参数RetT, ArgsT...传递给B. 一个签名转换类(AAdapter在上面的例子中)然后应用规则如何通过添加参数、更改它们的类型等从第一个签名自动生成第二个签名。A然后将保存这个生成的签名,并B拥有我提供的签名最初。但是,我想B提供一个invoke()带有 签名的方法A,从而完全隐藏A用户的整个方法签名。这就是为什么我需要访问模板参数类型A从内部B,以及为什么我不能简单地移除中产阶级AAdapter

4

2 回答 2

1

您问题的核心是将元组转换为参数包。

也许元组类型不是模板参数?在这种情况下,有一个简单的继承解决方案:

#include <vector>
#include <iostream>
#include <tuple>

template<typename... Types>
struct BImpl{
    typedef std::tuple<std::vector<Types>...> tuple_type;
    // maybe you will get a tuple type from some class templates. assume the 'tuple_type' is the result.
    // requirement: 'tuple_type' = std::tuple<SomeTypes...>
    // requirement: 'tuple_type' can be deduced definitely from template arguments 'Types...'.
    template<typename> // you can add other template arguments, even another std::tuple.
    struct OptCallHelper;
    template<typename... Args>
    struct OptCallHelper<std::tuple<Args...>>{
        auto dosomething(Args&&... args) /* const? noexcept? */{
            // do what you want...
            // requirement: you can definitely define the 'dosomething' here without any other informations.
            std::cout << "implement it here." << std::endl;
        }
    };
    typedef OptCallHelper<tuple_type> OptCall;
};

template<typename... Types>
struct B : private BImpl<Types...>::OptCall{
    typedef typename BImpl<Types...>::OptCall base;
    using base::dosomething;
    // obviously, you can't change the implementation here.
    // in other words, the definition of 'dosomething' can only depend on template arguments 'Types...'.
};


int main(){
    B<int, float> b;
    b({}, {}); // shows "implement it here."
    return 0;
}

你可以做你想做的事BImpl,然后B改用它。

   // This needs to have the same method signature (return type and argument types) as AAdapter::doSomething()
   template <size_t... Index>
   typename AAdapter::ReturnType doSomething (
       typename std::tuple_element<Index, typename AAdapter::ArgTypes>::type... args
   ) {
       return a.doSomething(args...);
   }

对于AAdaptor,我想你只是想要dosomethingin的接口A,你可以推导出来:

#include <iostream>

template<typename...>
struct AAdaptor{
    int dosomething(){
        std::cout << "???" << std::endl;
        return 0;
    }
};
// ignore the implementation of AAdaptor and A.
// just consider of how to get the interface of 'dosomething'.

template<typename... Types>
struct BImpl{
    typedef AAdaptor<Types...> value_type;
    typedef decltype(&value_type::dosomething) function_type;
    // attention: it won't work if 'AAdaptor::dosomething' is function template or overloaded.
    //            in this case, you should let A or AAdaptor give a lot of tuples to declare 'dosomething', referring to the first solution.

    

    template<typename>
    struct OptCallHelper;
    template<typename Ret, typename Klass, typename... Args>
    struct OptCallHelper<Ret(Klass::*)(Args...)>{
        value_type data;
        Ret dosomething(Args... args){
            return data.dosomething(args...);
        }
    };
    // attention: 'Ret(Klass::*)(Args...)' is different from 'Ret(Klass::*)(Args...) const', 'noexcept' as well in C++17.
    //            even Ret(Klass::*)(Args..., ...) is also different from them.
    //            you have to specialize all of them.

    typedef OptCallHelper<function_type> OptCall;
};

template<typename... Types>
struct B : BImpl<Types...>::OptCall{
    typedef typename BImpl<Types...>::OptCall base;
    using base::dosomething;
};


int main(){
    B<int, float> b;
    b(); // shows "???"
    return 0;
}

如果此代码与您的要求之间存在一些差异,请尝试举另一个示例来暗示您的一些实现。目前还不清楚什么B得到和应该做什么。

于 2020-08-14T13:05:38.543 回答
-1

这演示了如何从元组中获取具有参数类型的函数:

#include <iostream>
#include <tuple>
#include <utility>

template <
        typename ArgTuple
>
class B_Class {};

template <typename... ArgTypes>
class B_Class<std::tuple<ArgTypes...> > {
public:
        static void b(
                ArgTypes...
        ) {
                std::cout << "successful call" << std::endl;
        }
};

int main() {
        using ArgTypes = std::tuple<int, char, float, double>;
        int i; char c; float f; double d;
        B_Class<ArgTypes>::b(i, c, f, d);
}

这会在运行时编译并打印“成功调用”。

于 2020-08-12T04:03:04.247 回答