2

I am trying to work with an enum where one of the enum values is defined as a macro.

enum class Method
{
  GET, 
  POST, 
  PUT, 
  HEAD 
  // I want to add DELETE
};

This enum is being used ahead where values are being converted to string.

But when I add DELETE it says this is defined as a macro in one of the .h files I am using in the class - winnt.h.

In winnt.h, DELETE is defined as:

#define DELETE                           (0x00010000L)

The compiler gives an error saying 'expected an identifier'.

I don't want to change my code WHERE the enum is being used and converted to a string.

How can I add this DELETE value to my enum?

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
Jasmeet
  • 1,315
  • 11
  • 23
  • please post a [mcve] of the code that has the error and include the error message in the question – 463035818_is_not_an_ai May 09 '23 at 07:32
  • what is `DELETE` defined as ? – 463035818_is_not_an_ai May 09 '23 at 07:33
  • what do you expect to get from using `(0x00010000L)` as the name for an enumerator? – 463035818_is_not_an_ai May 09 '23 at 07:36
  • I know that DELETE is getting replaced by (0x00010000L) when I try using it as enum value. As the macro definition is something I cannot handle, is there a way to bypass the definition when using it in enum – Jasmeet May 09 '23 at 07:42
  • 1
    @churill yes, more context has been added in the meantime, though it was my misunderstanding from the start ;) – 463035818_is_not_an_ai May 09 '23 at 07:43
  • 1
    This problem goes away if we stop shouting. You use a scoped C++ enum already, `get`, `post` and such would work beautifully since you need to qualify them anyway. – StoryTeller - Unslander Monica May 09 '23 at 07:47
  • 2
    @StoryTeller-UnslanderMonica coincidentally, no. `delete` is a keyword ;) – 463035818_is_not_an_ai May 09 '23 at 07:50
  • 1
    @463035818_is_not_a_number - Wise cracking does not detract from the more general point of macro clashing – StoryTeller - Unslander Monica May 09 '23 at 08:04
  • @StoryTeller its not OPs macro, so changing their spelling wont make the issue go away. Though, I get your point, the issue is the macro (be it OPs or not) – 463035818_is_not_an_ai May 09 '23 at 08:13
  • 1
    Useful rule of thumb since time immemorial: if it's not a macro, don't make it look like a macro. – molbdnilo May 09 '23 at 08:21
  • 2
    First write your code as a static library (with all C++ code in namespaces), make sure this library is independent from windows header files. (If you need OS calls, then make those calls through an abstract baseclass/interface). Then make a seperate project to build and link your exe or dll (and OS specific implementation of your abstract baseclass). Added bonus you're one step closer to unit testability (and portability) – Pepijn Kramer May 09 '23 at 08:24
  • Uppercase is generally used for macros. You'd better not use it otherwise. But, since the lower case `delete` is a keyword, you're gonna need another name; `del` is an abbreviation fitting the less-than-4-character criterion that other values already satisfy. – Red.Wave May 09 '23 at 09:44
  • Or, you can simply use `Delete` instead, since macros and keywords are case-sensitive. Or, incorporate the fact that these are HTTP methods, like `HttpDelete`, etc – Remy Lebeau May 09 '23 at 14:40

1 Answers1

7

DELETE is a pre-existing Standard Access Right flag used with various Win32 APIs, such as CreateFile().

Your only options to use your own DELETE value are to either:

  • Don't use winnt.h in the same translation unit as your enum.

  • #undef the existing DELETE macro before declaring your enum. See Can I redefine a C++ macro then define it back? and other similar questions for how to preserve the old macro value so you can restore it afterwards (note: not all compilers support doing this).

Otherwise, you will have to rename your enum value to something else that is not already in use.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770