我正在为Win32开发SQL ODBC API的封装,经常需要使用像GetXXXTextA
和GetXXXTextW
这样的多个函数。我想根据用户输入类型选择适当的GetA
或GetW
。我尝试了以下方法:
// test getterA
int _stdcall pruebaA (int, char*, const char*)
{ return 0; }
// test getterW
int _stdcall pruebaW(int, wchar_t*, const wchar_t*)
{ return 0; }
template<typename T>
struct only_char_or_wchar_t
{
using ct = std::enable_if_t<std::is_same<T, char>::value || std::is_same<T, wchar_t>::value, T>;
};
template<typename char_type> struct char_or_wchart_api: only_char_or_wchar_t<char_type>
{
constexpr static std::conditional_t<std::is_same<char_type, wchar_t>::value, int (_stdcall*)(int, wchar_t*, const wchar_t*) , int(_stdcall*)(int, char*, const char*)> prueba =
std::is_same<char_type, wchar_t>::value
?
::pruebaW :
::pruebaA;
};
int main () {
auto p2 = char_or_wchart_api<wchar_t>::prueba;
p2(0, nullptr, L"");
return 0;
}
但是,Visual Studio 2017一直在抱怨(在行“::pruebaA;
”):
错误 C2446:':':从'int (__stdcall *)(int,char*,const char*)'到'int (__stdcall *)(int,wchar_t*,const wchar_t*)'没有转换
即使智能感知将“调用”p2(...)
正确解析为(int,wchar_t*,const wchar_t*)
您有什么想法,这段代码可能出了什么问题?
pruebaA
和pruebaW
都具有相同的原型,这就是为什么我不能让编译器通过参数推导来选择。 - yo_gdgpruebaA()
和pruebaW()
具有相同的签名...问题就完全改变了,简单的重载无法解决。但是,在这一点上,三元运算符应该可以解决问题... - max66