[英]C++ Variable number of variadic template parameters
我有以下類型別名:
using VoidFunc = void (*)();
我想定義一個這樣的類:
class SomeClass {
public:
template<template<typename Return, typename... Args>... Funcs>
static constexpr typename std::vector<VoidFunc> registerFunctions(Funcs<Return, Args...>... funcs) {
return { ((VoidFunc)(funcs), ...) };
}
};
我知道這是無效的語法,因為Return
和Args
在其模板組之外無法解析。 目的是允許函數registerFunction
接受數量可變的函數,這些函數都具有不同的原型。 在實際的實現中,重要的是保留每個函數的類型信息以與其他constexpr
函數一起使用。 有沒有辦法在C ++ 17或更高版本中實現這一目標?
您實際上並不關心任何這些底層類型(至少不在此處顯示的代碼中)。 因此,請不要使用它。 您只關心這些都是函數指針:
typename <typename... F>
static std::vector<VoidFunc> registerFunctions(F*... funcs)
{
static_assert((std::is_function_v<F> && ...));
return { reinterpret_cast<VoidFunc>(funcs)... };
}
如果確實需要其他特征的簽名,則可以將funcs
每個元素傳遞到不同的函數模板中,然后重新推導那里的實際簽名。
請注意, typename
是不必要的存在,我們的constexpr
在C ++ 17,使本病的形成(因為你不能在C ++ 17有constexpr
std::vector
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.