我正在尝试解决我在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是正确的,或者我的代码应该编译?
auto
、constexpr
、内联类成员函数定义等的规则)仍然没有说明为什么这里的定义不算。不过我建议等待一个auto
/constexpr
专家出现。 - Lightness Races in Orbit