簡體   English   中英

我不知道為什么這個 static_assert() 代碼不起作用

[英]I don't know why this static_assert() code doesn't work

這是代碼:

#pragma once

#include <stdint.h>

namespace Detours
{
    static_assert(sizeof(uintptr_t) == sizeof(void *));
}

我收到此錯誤消息:

Error (active) E2783 expected a comma (the one-argument version of static_assert is not enabled in this mode)

自 C++17 起, static_assert聲明允許省略message參數。 ( cppreference )

您需要在您的編譯器中啟用 C++17,或者這樣完成message參數:

static_assert(sizeof(uintptr_t) == sizeof(void *), "The message you want to show.");

也可以看看

如何在 Visual Studio 中啟用 C++17 編譯?

C++17 標准中引入了(至少被微軟稱為)“簡潔的 static 斷言”的語言功能 - 即只有一個參數的static_assert() 在此之前,第二個參數(字符串文字,錯誤消息)是必需的。 因此,使用(例如)MSVC 和“/std:C++14”標志編譯代碼會出現以下錯誤:

錯誤 C2429:語言功能“簡潔 static 斷言”需要編譯器標志“/std:c++17”

clang-cl 給出:

警告:沒有消息的 static_assert 是 C++17 擴展 [-Wc++17-extensions]

要解決此問題,請切換編譯器以符合 C++17 標准,或者,如果您沒有這種可能性,請添加所需的第二個參數:

    static_assert(sizeof(uintptr_t) == sizeof(void*), "Wrong uintptr_t size!");

但請注意,即使這樣,也不能保證斷言會成功! uintptr_t類型只需要足夠大以正確容納指針; 不必是完全相同的大小。 請參閱:什么是 uintptr_t 數據類型

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM