简体   繁体   English

使用静态constexpr成员函数返回类内部的auto

[英]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 ). 我正在尝试解决我遇到的MSVC 2015中的错误(请参阅此问题: 函数签名的错误类型推导 )。

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 错误C3779:'wreg :: t_api :: fnc_open_key':返回'auto'的函数在定义之前不能使用

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 ? 问题:为什么MSVC正确或我的代码应该编译?

You can try use decltype on auto function deduction: 您可以尝试在自动功能扣除上使用decltype:

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

This is no longer an issue. 这不再是一个问题。 Bug is resolved in VS 2015 RTM. 在VS 2015 RTM中解决了错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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