如何以constexpr方式调用具有元组输入的模板静态类方法
How can a static constexpr class::method (int i1, int i2, int i3)
be invoked, having input data available as tuple
in一种constexpr方式。
默认方法使用 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);
这在运行时起作用,但无法编译constexpr
。如何实施?
How can a static constexpr class::method (int i1, int i2, int i3)
be invoked, having input data available as tuple<int, int, int>
in a constexpr way.
The default approach is using std::apply to apply each tuple element as argument to a function.
A minimal example to visualize, what I try to achieve looks like:
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);
this works at runtime, but fails to compile constexpr
. How can this be implemented?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
工作
test_functor
:问题:
constexpr
- 构造很好。operator()
不是const
- 主要问题,因为您无法在constexpr上调用非 -
对象。const
成员template
调用t :: test
- 请参阅此FAQ答案可以很好地解释依赖名称。在线demo
Working
test_functor
:The problems:
constexpr
-constructed just fine.operator()
was notconst
– the primary problem, as you can't invoke a non-const
member on aconstexpr
object.template
keyword when invokingT::test
– see this FAQ answer for a good explanation of dependent names.Online Demo