8

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)
10
  • 2
    Define "doesn't work". What outcome do you expect, and what do you observe instead? Commented Nov 29, 2020 at 1:37
  • 2
    Error (active) E2783 expected a comma (the one-argument version of static_assert is not enabled in this mode) that is a error that i got Commented Nov 29, 2020 at 1:42
  • 5
    static_assert requires a second parameter - the error message - before C++17. Apparently, C++17 is not enabled in your compiler. Either figure out how to enable it, or add the error message, as in static_assert(condition, "Error message here") Commented Nov 29, 2020 at 1:44
  • 2
    You probably want to add an error message anyways. Commented Nov 29, 2020 at 1:48
  • 2
    Enabling c++17 is in the project settings. Properties -> C/C++ ->Language-> "C++ Language Standard" Commented Nov 29, 2020 at 1:48

2 Answers 2

16

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?

Sign up to request clarification or add additional context in comments.

Comments

8

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.