使用 "auto" 将文字数字设置为 "INT_PTR"

Using "auto" to set a literal number as "INT_PTR"

示例:

void CAssignSelectedColumnDlg::SaveActionListToRegistry()
{
    const auto iSize = m_aryPtrActionColumnData.GetSize();
    for (INT_PTR i = 0; i < iSize; i++)
    {
    }
}

如果我使用 auto i,它将生成 int 而不是 INT_PTR。 (GetSize(0) returns 和 INT_PTR)。有可能以某种方式将值 (0) 指定为 INT_PTR 而无需指定变量类型吗?这有意义吗?

例如:for(auto i = 0ip ...).

根据https://docs.microsoft.com/en-us/windows/win32/winprog/windows-data-types

INT_PTR

A signed integer type for pointer precision. Use when casting a pointer to an integer to perform pointer arithmetic.

This type is declared in BaseTsd.h as follows:

#if defined(_WIN64) 
  typedef __int64 INT_PTR; 
#else 
  typedef int INT_PTR;
#endif

您是为一种特定配置编写的吗?然后你可以使用 Integer literal

如果您同时为 32 位和 64 位配置构建,恐怕您需要将类型指定为 INT_PTR

或者,您可以使用 User-defined literals

更新:

这对我有用(以防你还没有得到它)​​:

#include <Windows.h>
INT_PTR operator "" _ip(unsigned long long x) {
    return static_cast<INT_PTR>(x);
}

int main() {
    auto x = 1_ip;
}