Incorrect function signature type inference

I came across a strange phenomenon by developing a templated API to access the Windows registry.

I thought I was smart by “grabbing” the ascii and unicode versions of the windows API in static constexpr 'variabeles' from 2 t_api structures (t_api_A and t_api_W).

Everything compiled fine, but it doesn't work very much (exceptions when calling "captured functions"). So I used a code snippet (from Scott Meyers' book, Effective Modern C ++), to see the subtracted types. Apparently, if I put these “capture functions” into structures, this will not work, however a simple auto = ...; inside the function.

It’s clear that I am doing something wrong, but I don’t understand why my approach is wrong.

Here's the code (some code is commented out because it deliberately generates errors)

#include<Windows.h> //============================================================================== namespace wreg { //------------------------------------------------------------------------------ using t_oshandle = HKEY; struct t_api { static constexpr auto open_key = ::RegOpenKeyExA; // Tried all of these : // RegOpenKeyExA; &RegOpenKeyExA; (::RegOpenKeyExA); (RegOpenKeyExA); (&RegOpenKeyExA); // static constexpr auto close_key = ::RegCloseKey; }; //------------------------------------------------------------------------------ } // namespace wreg //============================================================================== template < typename T > struct type_deduced; // see Scott Meyers' "Effective Modern C++" #define TYPE_DEDUCED( nr , t ) type_deduced< t > dummy_ ## nr int main () { //type_deduced< decltype(RegOpenKeyExA) > s1; //TYPE_DEDUCED( 1 , decltype(RegOpenKeyExA) ); // 'dummy_1' uses undefined struct 'type_deduced<LSTATUS (HKEY,LPCSTR,DWORD,REGSAM,PHKEY)>' //TYPE_DEDUCED( 1a , decltype(::RegOpenKeyExA) ); // 'dummy_1a' uses undefined struct 'type_deduced<LSTATUS (HKEY,LPCSTR,DWORD,REGSAM,PHKEY)>' //TYPE_DEDUCED( 3 , decltype(wreg::t_api::open_key) ); // 'dummy_3' uses undefined struct 'type_deduced<LSTATUS (__stdcall *const )(HKEY,LPCSTR,DWORD,REGSAM,PHKEY)>' auto hk = wreg::t_oshandle{}; #define ORIGINAL_APPROACH 0 #ifdef ORIGINAL_APPROACH // faulty version auto res = wreg::t_api::open_key( HKEY_LOCAL_MACHINE ,"SOFTWARE" ,0 ,KEY_READ ,&hk ); if (res == ERROR_SUCCESS) { res = wreg::t_api::close_key( hk ); } #else // working version auto open_key = ::RegOpenKeyExA; auto res = open_key( HKEY_LOCAL_MACHINE ,"SOFTWARE" ,0 ,KEY_READ ,&hk ); if (res == ERROR_SUCCESS) { auto close_key = ::RegCloseKey; res = close_key( hk ); } #endif return 0; } //============================================================================== 
+2
source share
1 answer

Error resolved in RTM VS 2015.

+1
source

Source: https://habr.com/ru/post/988236/


All Articles