4

I'm migrating my code from windows to Linux. There is no type named "__int64" on Linux, so I tried to define it myself with alias. My code is as follows:

#include <cstdint>

#if !defined(__int64)
typedef int64_t __int64;
#endif

int main(int argc, char** argv)
{
    unsigned __int64 ii64 = 0; // Error
    return 0;
};

When I compiled it, I got the following error:

main.cpp: In function ‘int main(int, char**)’: main.cpp:10:20: error: expected initializer before ‘ii64’

I know that I can replace "unsigned __int64" with "uint64_t" or defining a new type, but why the preceding code cannot be compiled?

5
  • 2
    The word unsigned is not a qualifier. (The type unsigned int is not the type int with an added unsigned, it's a completely separate type.) You could always use a macro instead.
    – molbdnilo
    Mar 22, 2016 at 14:46
  • You are thinking of the typedef as a macro with relates substitution rules, but does not work that way.
    – marom
    Mar 22, 2016 at 14:47
  • Yes, you're right, but "define" is not working either.
    – DavidZhang
    Mar 22, 2016 at 14:52
  • If it worked, and I came along and decided I really like my LargeInteger class and instead wrote typedef int64_t LargeInteger, what would you expect to happen?
    – CompuChip
    Mar 22, 2016 at 14:57
  • furthermore, int64_t is not a type that can be 'qualified' with unsigned anyway
    – sp2danny
    Mar 22, 2016 at 15:29

4 Answers 4

4

See the standard 7.1.6/2:

As a general rule, at most one type-specifier is allowed in the complete decl-specifier-seq of a declaration or in a type-specifier-seq or trailing-type-specifier-seq. The only exceptions to this rule are the following:

signed or unsigned can be combined with char, long, short, or int.

Note that this doesn't mean unsigned can be combined with a typedef that might eventually be one of those types, it can only be combined with those other types directly.

Further, bear in mind that __int64 is reserved for the compiler so defining it yourself is technically illegal. You're much better off using the standard int64_t and uint64_t types.

1
  • Thank you for so specific answer!
    – DavidZhang
    Mar 22, 2016 at 14:59
2

Because typedef is not a macro. It is a new type! And you can not construct the types like this.

When you say unsigned int, it is not the same sematics as const int. const is a so-called type qualifier. It means, it does not specify a new type, but adds some property to already existing type. However, unsigned is not a type quialifier. unsigned int is a type on it's own.

3
  • This is an interesting question. macro-ness aside, I can't think of any reason that it wouldn't be valid to add the unsigned attribute, unless the compiler already considers the type to be completely defined and closed to any further attribute manipulation. A brief perusal of some of the standards documentation did not shed any light whatsoever on the subject, either pro or con.
    – Logicrat
    Mar 22, 2016 at 14:52
  • Ah. It looks like @MarkB has nailed it.
    – Logicrat
    Mar 22, 2016 at 14:53
  • I understand that typedef defines a new type. But macro define doesn't work either.
    – DavidZhang
    Mar 22, 2016 at 14:54
0

To start with, "unsigned int" is a type, but "unsigned int64_t" is not.

2
  • typedef uint64_t __int64; Is that not extremely confusing and misleading? This wouldn't pass code review for me. Mar 22, 2016 at 14:58
  • Agreed. Removed the ugly code. Thanks for the comment. Mar 22, 2016 at 15:04
0

Apart from other things, that's because there is no type unsiged int64_t. Macro doesn't work for the same reason. But if you #define __int64 int, this will work, because there is a type unsigned int.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.