How do I resolve this <unresolved overloaded function type> error when using std::function?
在下面的(工作)代码示例中,模板化的register_enum()函数用于迭代枚举并调用用户提供的回调,以将枚举值转换为C字符串。所有枚举都是在一个类中定义的,枚举到字符串的转换是通过一个静态到字符串(枚举)函数完成的。当一个类(如下面的明暗器类)有多个枚举并对应于重载的"cstring(enum)"函数时,编译器无法确定传递给"register"enum()的"cstring()函数"是正确的。我觉得代码解释的比我能解释的更好…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | #include <functional> #include <iostream> // Actual code uses Lua, but for simplification // I'll hide it in this example. typedef void lua_State; class widget { public: enum class TYPE { BEGIN = 0, WINDOW = BEGIN, BUTTON, SCROLL, PROGRESS, END }; static const char *to_cstring( const TYPE value ) { switch ( value ) { case TYPE::WINDOW: return"window"; case TYPE::BUTTON: return"button"; case TYPE::SCROLL: return"scroll"; case TYPE::PROGRESS: return"progress"; default: break; } return nullptr; } }; class shader { public: enum class FUNC { BEGIN = 0, TRANSLATE = BEGIN, ROTATE, SCALE, COLOR, COORD, END }; enum class WAVEFORM { BEGIN = 0, SINE = BEGIN, SQUARE, TRIANGLE, LINEAR, NOISE, END }; static const char *to_cstring( const FUNC value ) { switch ( value ) { case FUNC::TRANSLATE: return"translate"; case FUNC::ROTATE: return"rotate"; case FUNC::SCALE: return"scale"; case FUNC::COLOR: return"color"; case FUNC::COORD: return"coord"; default: break; } return nullptr; } static const char *to_cstring( const WAVEFORM value ) { switch ( value ) { case WAVEFORM::SINE: return"sine"; case WAVEFORM::SQUARE: return"square"; case WAVEFORM::TRIANGLE: return"triangle"; case WAVEFORM::LINEAR: return"linear"; case WAVEFORM::NOISE: return"noise"; default: break; } return nullptr; } }; // Increment an enum value. // My compiler (g++ 4.6) doesn't support type_traits for enumerations, so // here I just static_cast the enum value to int... Something to be fixed // later... template < class E > E &enum_increment( E &value ) { return value = ( value == E::END ) ? E::BEGIN : E( static_cast<int>( value ) + 1 ); } widget::TYPE &operator++( widget::TYPE &e ) { return enum_increment< widget::TYPE >( e ); } shader::FUNC &operator++( shader::FUNC &e ) { return enum_increment< shader::FUNC >( e ); } shader::WAVEFORM &operator++( shader::WAVEFORM &e ) { return enum_increment< shader::WAVEFORM >( e ); } // Register the enumeration with Lua template< class E > void register_enum( lua_State *L, const char *table_name, std::function< const char*( E ) > to_cstring ) { (void)L; // Not used in this example. // Actual code creates a table in Lua and sets table[ to_cstring( i ) ] = i for ( auto i = E::BEGIN; i < E::END; ++i ) { // For now, assume to_cstring can't return nullptr... const char *key = to_cstring( i ); const int value = static_cast<int>(i); std::cout << table_name <<"." << key <<" =" << value << std::endl; } } int main( int argc, char **argv ) { (void)argc; (void)argv; lua_State *L = nullptr; // Only one to_cstring function in widget class so this works... register_enum< widget::TYPE >( L,"widgets", widget::to_cstring ); // ... but these don't know which to_cstring to use. register_enum< shader::FUNC >( L,"functions", shader::to_cstring ); //register_enum< shader::WAVEFORM >( L,"waveforms", shader::to_cstring ); return 0; } |
编译器输出:
1 2 3 4 5 | $ g++ -std=c++0x -Wall -Wextra -pedantic test.cpp -o test && ./test test.cpp: In function ‘int main(int, char**)’: test.cpp:140:69: error: no matching function for call to ‘register_enum(lua_State*&, const char [10], <unresolved overloaded function type>)’ test.cpp:140:69: note: candidate is: test.cpp:117:7: note: template<class E> void register_enum(lua_State*, const char*, std::function<const char*(E)>) |
如何将正确的to-cstring函数传递给register_enum()?我知道我可以将个人重命名为"cstring()函数",但如果可能的话,我想避免这样做。也许我的设计很臭,你可以推荐一个更好的方法。
我的问题类似于使用模板调用重载函数(未解决的重载函数类型编译器错误)以及如何获取重载成员函数的地址?但到目前为止,我还不能将这些信息应用到我的具体问题上。
错误告诉您有两个可能的重载可以使用,编译器无法为您决定。另一方面,您可以通过使用强制转换来确定要使用哪个强制转换:
1 2 | typedef const char *(*func_ptr)( shader::FUNC ); register_enum< shader::FUNC >( L,"functions", (func_ptr)shader::to_cstring ); |
或者没有typedef(在难以读取的一行程序中):
1 2 | register_enum< shader::FUNC >( L,"functions", (const char *(*)( shader::FUNC ))shader::to_cstring ); |
*注意,在函数签名中,顶级
下一个问题是,为什么编译器本身找不到适当的重载?问题是,在对
在这种情况下,如果编译器不能在两个重载之间做出决定,您应该重命名其中一个重载!这将防止对函数的进一步模糊使用。