C++ MSVC '=' 无法解析函数重载
C++ MSVC '=' unable to resolve function overload
我有一个 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 &)'
我还没弄清楚
- 为什么编译器会抛出错误?
- 如何修复该代码?
有人可以回答我这 2 个问题并解释问题吗?
提前致谢
问题是使用变量 i
作为模板参数。正确的方法是使用整数序列,它提供一组 compile-time 常量,可用作模板参数:
#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" );
}
}
我有一个 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 &)'
我还没弄清楚
- 为什么编译器会抛出错误?
- 如何修复该代码?
有人可以回答我这 2 个问题并解释问题吗? 提前致谢
问题是使用变量 i
作为模板参数。正确的方法是使用整数序列,它提供一组 compile-time 常量,可用作模板参数:
#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" );
}
}