簡體   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