【发布时间】:2018-04-03 19:57:51
【问题描述】:
我正在为 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;”行):
Error C2446: ':': no conversion from 'int (__stdcall *)(int,char *,const char *)' to 'int (__stdcall *)(int,wchar_t *,const wchar_t *)'
即使智能感知在“调用”p2(.....) 到 (int, wchar_t*, const wchar_t*) 时正确解析
你知道这段代码有什么问题吗?
【问题讨论】:
标签: c++ templates winapi c++14 template-meta-programming