"Theoretical Practice" size_t and std:size_t may be different types of definitions, just defined as the same type

Source: Internet
Author: User

This is rarely realized, with 2 respectively corresponding to the C header file definition and C + + header file definitions, the day-to-day use has no effect because the included first will cause subsequent inclusion definition trigger conditions to compile, and often using namespace std; make no distinction between std:size_t. As a good code exporter, to be rigorous to understand, targeted processing, improve code quality, reduce the code development cycle.


Conclusion: C and C + + mixed, especially across the platform, must pay attention to the header file contains order, if it is C + +, the earliest contains C + + header file, if it is C, later contains C + + header files, this is a difference, do not guarantee complete equivalence.


The first part, influence:

There is no impact to the business logic, including typeID, because conditional compilation is the preprocessing phase complete and the compile phase is already facing the underlying type definition.

However, after all, two files are independent, in the case of Cross-platform compilation, type definitions only to ensure that the length is equal, two files may produce definition differences, resulting in type mismatch, such as

#include <iostream>
#include <typeinfo>
using namespace std;

typedef long Long  type1;
typedef int64_t TYPE2;

int main ()
{
        type1 s1 = 0;
        type2 s2 = 0;
        cout << sizeof (type1) << "<< sizeof (type2) << Endl;
        cout << (typeid (S1) = = typeid (s2)? "EQ": "NQ") << Endl;
        cout << typeid (S1). Name () << Endl;
        cout << typeid (S2). Name () << Endl;
        return 0;
}
Output
8 8
nq
x
L

This is not the same as logic, bit length, symbol, but different types.


The second part, verify the difference, view the definition source by creating a conflict error for the size_t redefinition

#include <stdio.h>
typedef bool size_t;
int main ()
{return
                0;
}

Compile error: main.cpp:3:14:error:conflicting declaration ' typedef BOOL SIZE_T '
typedef BOOL SIZE_T;
^
In file included from/usr/include/stdio.h:34:0,
From Main.cpp:1:
/opt/rh/devtoolset-2/root/usr/lib/gcc/x86_64-redhat-linux/4.8.2/include/stddef.h:212:23:error: ' size_t ' has a Previous declaration as ' typedef long unsigned int size_ '
typedef __size_type__ SIZE_T;
^
Make: * * * [main] Error 1
Continue tracking:
/opt/rh/devtoolset-2/root/usr/lib/gcc/x86_64-redhat-linux/4.8.2/include/stddef.h:209: #define __SIZE_TYPE__ Long unsigned int

So the C header file definition is: long unsigned int


Let's take a look at the C + + definition

#include <iostream>
using namespace std;
Namespace STD
{
        typedef bool size_t;
}
int main ()
{return
        0;
}
Compile an error
Main.cpp:7:15:error:conflicting declaration ' typedef BOOL STD::SIZE_T '
typedef BOOL SIZE_T;
^
In file included from/opt/rh/devtoolset-2/root/usr/include/c++/4.8.2/iostream:38:0,
From Main.cpp:2:
/opt/rh/devtoolset-2/root/usr/include/c++/4.8.2/x86_64-redhat-linux/bits/c++config.h:1857:26:error: ' Std::size_ T ' has a previous declaration as ' typedef long unsigned int std::size_t '
typedef __size_type__ SIZE_T;
^
Make: * * * [main] Error 1
Further tracking:
bits/c++config.h:1764 if __cplusplus
...
bits/c++config.h:1855 namespace Std
{
typedef __size_type__ SIZE_T;
typedef __ptrdiff_type__ PTRDIFF_T;


#if __cplusplus >= 201103L
typedef decltype (nullptr) nullptr_t;
#endif

}

This approach is very smart, direct use of the system __size_type__, so must and C consistent


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.