如何以 constexpr 方式调用具有元组输入的模板化静态类方法

Chr*_* G. 3 c++ constexpr c++20

如何调用 a ,以constexpr 方式static constexpr class::method (int i1, int i2, int i3)提供输入数据。tuple<int, int, int>

默认方法是使用std::apply将每个元组元素作为参数应用于函数。

一个最小的可视化示例,我试图实现的目标如下:

struct a {
    template <typename T>
    static constexpr void test(int i1, int i2, int i3) {
        // ...
    }
};

struct b : a {};
struct c {};

template <typename T>
struct test_functor {
    constexpr test_functor_t() {} // just for testing to express constexpr desire
    constexpr void operator()(auto... args) {
        T::test<c>(args...);
    }
};

constexpr std::tuple<int, int, int> tupl{ 1,2,3 };
constexpr test_functor<b> f;
std::apply(f, tupl);
Run Code Online (Sandbox Code Playgroud)

这在运行时有效,但无法编译constexpr。如何实施?

ild*_*arn 5

在职的test_functor

\n
template <typename T>\nstruct test_functor {\n    constexpr void operator()(auto... args) const {\n        T::template test<c>(args...);\n    }\n};\n
Run Code Online (Sandbox Code Playgroud)\n

问题:

\n
    \n
  1. 您的构造函数命名错误,最终不必要的 \xe2\x80\x93 没有构造函数,您的类型是一个聚合,可以constexpr很好地构造。
  2. \n
  3. 您的主要问题operator()不是const\xe2\x80\x93,因为您无法调用对象const上的非成员constexpr
  4. \n
  5. template调用T::test\xe2\x80\x93时缺少关键字,请参阅此常见问题解答以获得依赖名称的良好解释。
  6. \n
\n

Online Demo

\n