繁体   English   中英

带有推导参数的模板函数指针数组变量

[英]Template function pointer array variable with deduced arguments

我想使用函数指针数组而不指定指针类型。 声明它有效,但我无法在不传递模板参数的情况下访问内容,这违背了目的。 有没有办法解决这个问题? 我正在使用 C++17。

std::function 在这种情况下有效,但我想避免使用它。

#include <functional>

bool f(int x);
bool g(int x);

static std::function<decltype(f)> fps[] {f, g};

template <typename T, typename... Args>
constexpr T (*fpsa[])(Args...) {f, g};

int main() {
    fps[0](1); // OK
    fpsa<bool,int>[0](1); // OK
    fpsa[0](1); // error: use of variable template 'fpsa' requires template arguments
}

我想使用函数指针数组而不指定指针类型。

在这种情况下,您可以将类模板参数推导与std::array

constexpr auto fpsa = std::array{f, g};

那应该就够了。 无需将其设为模板变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM