<p>Is there a way to have a 64 bit enum in C++?  Whilst refactoring some code I came across bunch of #defines which would be better as an enum, but being greater than 32 bit causes the compiler to error.</p>

<p>For some reason I thought the following might work:</p>

<pre><code>enum MY_ENUM : unsigned __int64  
{  
    LARGE_VALUE = 0x1000000000000000,  
};
</code></pre>

[{'answer_id': 76661, 'author': 'Doug T.', 'author_id': 8123, 'author_profile': 'https://Stackoverflow.com/users/8123', 'pm_score': 0, 'selected': False, 'text': "<p>An enum in C++ can be any integral type. You can, for example, have an enum of chars. IE:</p>\n\n<pre><code>enum MY_ENUM\n{\n   CHAR_VALUE = 'c',\n};\n</code></pre>\n\n<p>I would <em>assume</em> this includes __int64. Try just</p>\n\n<pre><code>enum MY_ENUM\n{\n   LARGE_VALUE = 0x1000000000000000,\n};\n</code></pre>\n\n<p>According to my commenter, sixlettervariables, in C the base type will be an int always, while in C++ the base type is whatever is large enough to fit the largest included value. So both enums above should work.</p>\n"}, {'answer_id': 76683, 'author': 'Ferruccio', 'author_id': 4086, 'author_profile': 'https://Stackoverflow.com/users/4086', 'pm_score': 5, 'selected': True, 'text': "<p>I don't think that's possible with C++98. The underlying representation of enums is up to the compiler. In that case, you are better off using:</p>\n\n<pre><code>const __int64 LARGE_VALUE = 0x1000000000000000L;\n</code></pre>\n\n<p>As of C++11, it is possible to use enum classes to specify the base type of the enum:</p>\n\n<pre><code>enum class MY_ENUM : unsigned __int64 {\n    LARGE_VALUE = 0x1000000000000000ULL\n};\n</code></pre>\n\n<p>In addition enum classes introduce a new name scope. So instead of referring to <code>LARGE_VALUE</code>, you would reference <code>MY_ENUM::LARGE_VALUE</code>.</p>\n"}, {'answer_id': 76705, 'author': 'Torlack', 'author_id': 5243, 'author_profile': 'https://Stackoverflow.com/users/5243', 'pm_score': 1, 'selected': False, 'text': '<p>Since you are working in C++, another alternative might be </p>\n\n<pre><code>const __int64 LARVE_VALUE = ...\n</code></pre>\n\n<p>This can be specified in an H file.</p>\n'}, {'answer_id': 76756, 'author': 'INS', 'author_id': 13136, 'author_profile': 'https://Stackoverflow.com/users/13136', 'pm_score': 2, 'selected': False, 'text': "<p>If the compiler doesn't support 64 bit enums by compilation flags or any other means I think there is no solution to this one.</p>\n\n<p>You could create something like in your sample something like:</p>\n\n<pre><code>namespace MyNamespace {\nconst uint64 LARGE_VALUE = 0x1000000000000000;\n};\n</code></pre>\n\n<p>and using it just like an enum using </p>\n\n<pre><code>MyNamespace::LARGE_VALUE \n</code></pre>\n\n<p>or </p>\n\n<pre><code>using MyNamespace;\n....\nval = LARGE_VALUE;\n</code></pre>\n"}, {'answer_id': 76757, 'author': 'ugasoft', 'author_id': 10120, 'author_profile': 'https://Stackoverflow.com/users/10120', 'pm_score': 1, 'selected': False, 'text': '<p>your snipplet of code is not c++ standard:</p>\n\n<blockquote>\n  <p>enum MY_ENUM : unsigned __int64  </p>\n</blockquote>\n\n<p>does not make sense.</p>\n\n<p>use const __int64 instead, as Torlack suggests</p>\n'}, {'answer_id': 76980, 'author': 'Leon Timmermans', 'author_id': 4727, 'author_profile': 'https://Stackoverflow.com/users/4727', 'pm_score': 4, 'selected': False, 'text': '<p>C++11 supports this, using this syntax:</p>\n\n<pre><code>enum class Enum2 : __int64 {Val1, Val2, val3};\n</code></pre>\n'}, {'answer_id': 81531, 'author': 'MSalters', 'author_id': 15416, 'author_profile': 'https://Stackoverflow.com/users/15416', 'pm_score': 2, 'selected': False, 'text': '<p>The answers refering to <code>__int64</code> miss the problem. The enum <em>is</em> valid in all C++ compilers that have a true 64 bit integral type, i.e. any C++11 compiler, or C++03 compilers with appropriate extensions. Extensions to C++03 like <code>__int64</code> work differently across compilers, including its suitability as a base type for enums. </p>\n'}, {'answer_id': 1470527, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>In MSVC++ you can do this:  </p>\n\n<p>enum MYLONGLONGENUM:__int64 { BIG_KEY=0x3034303232303330, ... };</p>\n'}, {'answer_id': 2630851, 'author': 'mloskot', 'author_id': 151641, 'author_profile': 'https://Stackoverflow.com/users/151641', 'pm_score': 3, 'selected': False, 'text': '<p>The current draft of so called <a href="http://en.wikipedia.org/wiki/C%2B%2B0x" rel="noreferrer">C++0x</a>, it is <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3092.pdf" rel="noreferrer">n3092</a> says in <strong>7.2 Enumeration declarations</strong>, paragraph 6:</p>\n\n<blockquote>\n  <p>It is implementation-defined which\n  integral type is used as the\n  underlying type except that the\n  underlying type shall not be larger\n  than int unless the value of an\n  enumerator cannot fit in an int or\n  unsigned int.</p>\n</blockquote>\n\n<p>The same paragraph also says:</p>\n\n<blockquote>\n  <p>If no integral type can represent all\n  the enumerator values, the enumeration\n  is ill-formed.</p>\n</blockquote>\n\n<p>My interpretation of the part  <strong>unless the value of an enumerator cannot fit in an int or unsigned int</strong> is that it\'s perfectly valid and safe to initialise enumerator with 64-bit integer value as long as there is 64-bit integer type provided in a particular C++ implementation.</p>\n\n<p>For example:</p>\n\n<pre><code>enum MyEnum\n{\n    Undefined = 0xffffffffffffffffULL\n};\n</code></pre>\n'}, {'answer_id': 13755860, 'author': 'human.js', 'author_id': 1198898, 'author_profile': 'https://Stackoverflow.com/users/1198898', 'pm_score': 1, 'selected': False, 'text': '<p>Enum type is normally determined by the data type of the first enum initializer. If the value should exceed the range for that integral datatype then c++ compiler will make sure it fits in by using a larger integral data type.If compiler finds that it does not belong to any of the integral data type then compiler will throw error.\nRef: <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf" rel="nofollow">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf</a>\n<br>Edit: However this is purely depended on machine architecture</p>\n'}]
['https://Stackoverflow.com/questions/76624', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/9236/']