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

Viewed 3891

This is the code:

#pragma once

#include <stdint.h>

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

I'm getting this error message:

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

The static_assert declaration allows the message parameter to be omitted since C++17. (cppreference)

You need to enable C++17 in your compiler, or complete the message parameter this way:

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

See also

How to enable C++17 compiling in Visual Studio?

The language feature known (by Microsoft, at least) as "terse static assert" - that is, a static_assert() with only one argument - was introduced in the C++17 Standard. Before that, the second argument (a string literal, the error message) is required. So, compiling your code with (for example) MSVC and the "/std:C++14" flag, gives this error:

error C2429: language feature 'terse static assert' requires compiler flag '/std:c++17'

And clang-cl gives:

warning : static_assert with no message is a C++17 extension [-Wc++17-extensions]

To fix this, either switch your compiler to conform to the C++17 Standard or, if you don't have that possibility, add the required second argument:

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

But note, even with that, there is no guarantee that the assertion will succeed! The uintptr_t type is required only to be of sufficient size to correctly accommodate a pointer; it does not have to be the exact same size. See: What is uintptr_t data type.

Related