簡體   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