Home Backend Development C++ Why Does `int64_t` Behave Differently in 64-bit GCC Compiles?

Why Does `int64_t` Behave Differently in 64-bit GCC Compiles?

Oct 30, 2024 pm 07:19 PM

Why Does `int64_t` Behave Differently in 64-bit GCC Compiles?

Long Long Int vs. Long Int vs. Int64_t in C

C type traits can exhibit quirky behavior, particularly with signed 64-bit integer types. Here's an exploration of why:

In both 32-bit and 64-bit compiles (GCC and MSVC), the following program behaves as expected:

<code class="cpp">#include &lt;iostream&gt;
#include &lt;cstdint&gt;

template &lt;typename T&gt;
bool is_int64() { return false; }

template &lt;&gt;
bool is_int64&lt;int64_t&gt;() { return true; }

int main()
{
    std::cout &lt;&lt; &quot;int:\t&quot; &lt;&lt; is_int64&lt;int&gt;() &lt;&lt; std::endl;
    std::cout &lt;&lt; "int64_t:\t" &lt;&lt; is_int64&lt;int64_t&gt;() &lt;&lt; std::endl;
    std::cout &lt;&lt; &quot;long int:\t&quot; &lt;&lt; is_int64&lt;long int&gt;() &lt;&lt; std::endl;
    std::cout &lt;&lt; &quot;long long int:\t&quot; &lt;&lt; is_int64&lt;long long int&gt;() &lt;&lt; std::endl;

    return 0;
}</code>
Copy after login

The output correctly indicates that int64_t, long int, and long long int are all equivalent 64-bit integer types.

However, a 64-bit GCC compile produces a different result:

int:           0
int64_t:       1
long int:      1
long long int: 0
Copy after login

This surprising behavior stems from the C standard library definition of int64_t:

<code class="cpp"># if __WORDSIZE == 64
typedef long int  int64_t;
# else
__extension__
typedef long long int  int64_t;
# endif</code>
Copy after login

In a 64-bit compile, int64_t is defined as long int, not long long int. This means that the is_int64() template specialization that checks for int64_t will match long int instead of long long int.

Solution using Partial Template Specialization:

To address this issue, you can use partial template specialization to explicitly define is_int64() for long long int:

<code class="cpp">#if defined(__GNUC__) &amp;&amp; (__WORDSIZE == 64)
template &lt;&gt;
bool is_int64&lt;long long int&gt;() { return true; }
#endif</code>
Copy after login

However, this solution is dependent on the compiler and can be tedious to implement for every affected type.

Alternative Solution using Boost:

Boost provides a more elegant solution with the boost::is_same variadic template:

<code class="cpp">#include &lt;boost/type_traits/is_same.hpp&gt;

template &lt;typename T&gt;
bool is_int64_boost() { return boost::is_same&lt;T, int64_t&gt;::value; }

int main()
{
    std::cout &lt;&lt; &quot;int:\t&quot; &lt;&lt; is_int64_boost&lt;int&gt;() &lt;&lt; std::endl;
    std::cout &lt;&lt; "int64_t:\t" &lt;&lt; is_int64_boost&lt;int64_t&gt;() &lt;&lt; std::endl;
    std::cout &lt;&lt; &quot;long int:\t&quot; &lt;&lt; is_int64_boost&lt;long int&gt;() &lt;&lt; std::endl;
    std::cout &lt;&lt; "long long int:\t" &lt;&lt; is_int64_boost&lt;long long int&gt;() &lt;&lt; std::endl;

    return 0;
}</code>
Copy after login

This approach will correctly identify all equivalent 64-bit integer types, regardless of their exact representation in the standard library.

The above is the detailed content of Why Does `int64_t` Behave Differently in 64-bit GCC Compiles?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the types of values ​​returned by c language functions? What determines the return value? What are the types of values ​​returned by c language functions? What determines the return value? Mar 03, 2025 pm 05:52 PM

What are the types of values ​​returned by c language functions? What determines the return value?

Gulc: C library built from scratch Gulc: C library built from scratch Mar 03, 2025 pm 05:46 PM

Gulc: C library built from scratch

What are the definitions and calling rules of c language functions and what are the What are the definitions and calling rules of c language functions and what are the Mar 03, 2025 pm 05:53 PM

What are the definitions and calling rules of c language functions and what are the

Where is the return value of the c language function stored in memory? Where is the return value of the c language function stored in memory? Mar 03, 2025 pm 05:51 PM

Where is the return value of the c language function stored in memory?

C language function format letter case conversion steps C language function format letter case conversion steps Mar 03, 2025 pm 05:53 PM

C language function format letter case conversion steps

distinct usage and phrase sharing distinct usage and phrase sharing Mar 03, 2025 pm 05:51 PM

distinct usage and phrase sharing

How does the C   Standard Template Library (STL) work? How does the C Standard Template Library (STL) work? Mar 12, 2025 pm 04:50 PM

How does the C Standard Template Library (STL) work?

How do I use algorithms from the STL (sort, find, transform, etc.) efficiently? How do I use algorithms from the STL (sort, find, transform, etc.) efficiently? Mar 12, 2025 pm 04:52 PM

How do I use algorithms from the STL (sort, find, transform, etc.) efficiently?

See all articles