繁体   English   中英

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

[英]use of a static constexpr member function returning auto inside class

我正在尝试解决我遇到的MSVC 2015中的错误(请参阅此问题: 函数签名的错误类型推导 )。

所以我想出了这个:

#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;
}

但它不会因为编译而编译

错误C3779:'wreg :: t_api :: fnc_open_key':返回'auto'的函数在定义之前不能使用

我不明白。 它在我使用它时明确定义。 除此之外,在类中通常可以在定义/声明之前使用类定义的本地名称。

问题:为什么MSVC正确或我的代码应该编译?

您可以尝试在自动功能扣除上使用decltype:

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

这不再是一个问题。 在VS 2015 RTM中解决了错误。

暂无
暂无

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

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