简体   繁体   中英

use of a static constexpr member function returning auto inside class

I'm trying to workaround a bug in MSVC 2015 that I encountered (see this question : wrong type deduction of function signature ).

So I came up with this :

#include<Windows.h>

namespace wreg {

using t_oshandle     = HKEY;

struct t_api
{
    static constexpr 
    auto fnc_open_key ()     { return ::RegOpenKeyExA; }

    //this doesn't compile :
    static constexpr auto open_key   = fnc_open_key();

    //these don't compile either:
    //static constexpr decltype(fnc_open_key()) open_key     = fnc_open_key();
    //static constexpr decltype(::RegOpenKeyExA) open_key    = fnc_open_key();
};

//this does compiles and runs :
constexpr auto open_key  = t_api::fnc_open_key();

} // namespace wreg


//int main( int argc ,_TCHAR* argv[] );
{
    auto     hk = wreg::t_oshandle{};
    auto     res = wreg::t_api::open_key( HKEY_LOCAL_MACHINE ,"SOFTWARE" ,0 ,KEY_READ ,&hk );
    //auto   res = wreg::open_key( HKEY_LOCAL_MACHINE ,"SOFTWARE" ,0 ,KEY_READ ,&hk );

    if (res == ERROR_SUCCESS)
    {
        res = ::RegCloseKey( hk );
    }
    return 0;
}

but it doesn't compile because of

error C3779: 'wreg::t_api::fnc_open_key': a function that returns 'auto' cannot be used before it is defined

I don't get that. It's clearly defined at the point I use it. And besides that ,within a class usually names local to the class definition can be use before its definition/declaration.

Question : Why is MSVC right or should my code compile ?

You can try use decltype on auto function deduction:

auto fnc_open_key () -> decltype(::RegOpenKeyExA) {
       return ::RegOpenKeyExA;
}

This is no longer an issue. Bug is resolved in VS 2015 RTM.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM