我有一個 API,我可以將多個函式指標注冊為回呼。但是,我需要在呼叫回呼時跟蹤其他資料(在此示例中為索引)。我想要做的是在編譯時生成一堆方法來保存這些額外的資料。我的代碼如下:
#include <iostream>
#include <vector>
#include <sstream>
#include <array>
// API function format
typedef void ( *Function )( const std::string& msg );
// My function accepting the API interface an additional index
void callback( const size_t index, const std::string& msg ) {
std::cout << "(" << index << "): " << msg << std::endl;
}
// Wrapper for my function in API format generating the index
template <size_t METHOD_INDEX>
void wrapper( const std::string& msg ) {
return callback( METHOD_INDEX, msg );
}
// Constexpr Array which should be built on compile time, containing all wrappers
template <size_t SIZE>
struct Array {
constexpr Array() : arr() {
for ( auto i = 0; i < SIZE; i ) {
arr[ i ] = wrapper<i>; // Error at this line
}
}
size_t size() const {
return SIZE;
}
void ( *arr[ SIZE ] )( const std::string& );
};
int main() {
static constexpr auto wrappers = Array<5>();
// Emulating registering wrapper functions at the API
const auto NUM_CALLBACKS = 5;
std::vector<Function> apiCallbacks( NUM_CALLBACKS );
for ( auto i = 0; i < NUM_CALLBACKS; i ) {
apiCallbacks[ i ] = wrappers.arr[ i ];
}
// Emulating API is calling registered functions
for ( auto i = 0; i < NUM_CALLBACKS; i ) {
apiCallbacks[ i ]( "Test" );
}
}
在源代碼中標記的行處,編譯器 (MSVC x64 16.8) 會拋出錯誤:
main.cpp(25,1): error C2563: mismatch in formal parameter list
main.cpp(23): message : while compiling class template member function 'Array<5>::Array(void)'
main.cpp(37): message : see reference to class template instantiation 'Array<5>' being compiled
main.cpp(25,1): error C2568: '=': unable to resolve function overload
main.cpp(25,1): message : could be 'void wrapper(const std::string &)'
我還沒弄清楚
- 為什么編譯器會拋出錯誤?
- 如何修復該代碼?
有人可以回答我這兩個問題并解釋問題嗎?提前致謝
uj5u.com熱心網友回復:
問題是使用變數i作為模板引數。正確的方法是使用整數序列,它提供了一組可用作模板引數的編譯時常量:
#include <iostream>
#include <vector>
#include <sstream>
#include <array>
#include <utility>
#include <cstddef>
// API function format
typedef void ( *Function )( const std::string& msg );
// My function accepting the API interface an additional index
void callback( const size_t index, const std::string& msg ) {
std::cout << "(" << index << "): " << msg << std::endl;
}
// Wrapper for my function in API format generating the index
template <size_t METHOD_INDEX>
void wrapper( const std::string& msg ) {
return callback( METHOD_INDEX, msg );
}
template <::std::size_t... x_index>
constexpr auto make_wrappers_impl(::std::index_sequence<x_index...>)
{
return ::std::array<Function, sizeof...(x_index)>{&wrapper<x_index>...};
}
template <::std::size_t x_size>
constexpr auto make_wrappers(void)
{
return make_wrappers_impl(::std::make_index_sequence<x_size>());
}
int main() {
static constexpr auto wrappers{make_wrappers<5>()};
// Emulating registering wrapper functions at the API
const auto NUM_CALLBACKS = 5;
std::vector<Function> apiCallbacks( NUM_CALLBACKS );
for ( auto i = 0; i < NUM_CALLBACKS; i ) {
apiCallbacks[ i ] = wrappers[ i ];
}
// Emulating API is calling registered functions
for ( auto i = 0; i < NUM_CALLBACKS; i ) {
apiCallbacks[ i ]( "Test" );
}
}
在線編譯器
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/443095.html
下一篇:在C 中一行中讀取N個元素
