CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1

    can i force compiler to use 64 bit int?

    Wanna be sure what var declared as int is _int64.

    May be some define symbol for it?

  2. #2
    Join Date
    Jan 2009
    Posts
    1,689

    Re: can i force compiler to use 64 bit int?

    #define int _int64
    assert(sizeof(int) == 8, "int the wrong size")

    Why are you not going to use _int64?

  3. #3
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: can i force compiler to use 64 bit int?

    If all int are treated as _int64 then will that not cause problems with the headers of libraries which you link to ?

    If you just want int to be 64 bit in your own code, and always want it to be 64 bit, then I would typedef my own int type:

    Code:
    namespace MyCompanyName
    {
    typedef _int64 Int64;
    }
    My hobby projects:
    www.rclsoftware.org.uk

  4. #4
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: can i force compiler to use 64 bit int?

    Quote Originally Posted by ninja9578 View Post
    #define int _int64
    That's absolutely illegal. You are not permitted to define macros with names identical to language keywords.

    Quote Originally Posted by ninja9578 View Post
    assert(sizeof(int) == 8, "int the wrong size")
    assert takes one argument; I think you meant assert((expression) && "string-literal"), instead.
    In either case, a run-time assertion is less than optimal. This sort of thing should be enforced at compile-time. A simple static assertion will handle this more gracefully.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: can i force compiler to use 64 bit int?

    Quote Originally Posted by ninja9578 View Post
    #define int _int64
    assert(sizeof(int) == 8, "int the wrong size")
    Use a static assert, so that any attempt to use ints that are not 64 bit won't compile:
    Code:
    char dummy[sizeof(int) == 8];
    This simple line will compile OK if ints are 8 bytes, and will fail if they are not.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Jul 2002
    Posts
    2,543

    Re: can i force compiler to use 64 bit int?

    Quote Originally Posted by Plasmator View Post
    That's absolutely illegal. You are not permitted to define macros with names identical to language keywords.
    Why? This program compiles and runs:

    Code:
    #define short std::cout
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        short << "OK" << std::endl;
        return 0;
    }
    Replacing short by int diesn't compile, because preprocessor converts function header to:
    std::cout _tmain(std::cout argc, _TCHAR* argv[])

    We can define any macro, but result may be very interesting... Of course, making int macro destroys the program completely. Agree with Zaccheus, typedef is the best solution.

  7. #7
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: can i force compiler to use 64 bit int?

    Quote Originally Posted by Alex F View Post
    Why? This program compiles and runs:
    It doesn't matter what your compiler is willing to swallow -- this sort of thing is explicitly forbidden by the standard.

    If you're writing portable and well-defined code, you mustn't do this.

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: can i force compiler to use 64 bit int?

    Quote Originally Posted by Alex F
    Why?
    I had the impression that the rule was broadly applicable, but it appears to be applicable only when a standard header is included:
    Quote Originally Posted by C++03 Section 17.4.3.1.1 Paragraph 2
    A translation unit that includes a header shall not contain any macros that define names declared or defined in that header. Nor shall such a translation unit define macros for names lexically identical to keywords.
    Quote Originally Posted by Alex F
    This program compiles and runs:
    It is not standard C++. To make it standard C++, we would likely #include <iostream> (besides changing _tmain(int argc, _TCHAR* argv[]) to main(int argc, char* argv[])), in which case the rule I cited comes into effect and the program has undefined behaviour.

    Of course, I may have missed out a more broadly applicable rule, and perhaps this qualifies:
    Quote Originally Posted by C++03 Section 2.10 Paragraph 2
    In addition, some identifiers are reserved for use by C++ implementations and standard libraries and shall not be used otherwise; no diagnostic is required.
    though it then seems strange to forbid defining macros for names lexically identical to keywords in a more specific case if this rule already covers the general case.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Aug 2007
    Posts
    858

    Re: can i force compiler to use 64 bit int?

    in which case the rule I cited comes into effect and the program has undefined behaviour.
    Is that really the case though? While this specific scenario may not technically be defined, the rules for how the preprocessor handles macro substitution are clearly defined, so unless a particular vendor explicitly blocks macros from having the same names as keywords, the preprocessor will do what it always does. It also doesn't help that the other rules in that section are essentially unenforceable. How exactly are you going to prevent someone from defining a macro with the a name declared in a header?

    Consider the following, for example - accepted by Comeau and MSVC9 without complaint

    Code:
    #include <iostream>
    #include <cstdlib>
    
    #undef NULL // illegal
    #define void std::cout // illegal
    #define malloc std::endl // illegal
    
    int main()
    {
      void << "test" << malloc;
      return 0;
    }

  10. #10
    Join Date
    Jul 2002
    Posts
    2,543

    Re: can i force compiler to use 64 bit int?

    Quote Originally Posted by Plasmator View Post
    It doesn't matter what your compiler is willing to swallow -- this sort of thing is explicitly forbidden by the standard.

    If you're writing portable and well-defined code, you mustn't do this.
    Of course I don't do this, but the point is what code is working, and what code doesn't work. Macros are handled by preprocessor, and it didn't read your post.

  11. #11
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: can i force compiler to use 64 bit int?

    Quote Originally Posted by Speedo
    Is that really the case though? While this specific scenario may not technically be defined, the rules for how the preprocessor handles macro substitution are clearly defined, so unless a particular vendor explicitly blocks macros from having the same names as keywords, the preprocessor will do what it always does. It also doesn't help that the other rules in that section are essentially unenforceable. How exactly are you going to prevent someone from defining a macro with the a name declared in a header?
    Indeed. I wonder if this is a case of where the standard committee wanted to give more leeway to compiler vendors, even if the leeway is not actually used by modern compilers. But it does not square with the rule only being in effect when a standard header is included, since that would only concern standard library implementers, who are not necessarily the compiler vendors.

    Quote Originally Posted by Speedo
    Consider the following, for example - accepted by Comeau and MSVC9 without complaint
    Quote Originally Posted by Alex F
    Of course I don't do this, but the point is what code is working, and what code doesn't work. Macros are handled by preprocessor, and it didn't read your post.
    Using an apparently working program to prove that the code works fails in the presence of undefined behaviour, unless you exhaustively test all current and future compilers (or simply restrict your statement to say that it works for certain compilers, with the given versions and configurations). Plasmator's post does not matter, but the C++ Standard matters.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured