redefinition of C + +

Source: Internet
Author: User

Write the following two lines of code in the header file:

typedef int X;

typedef int X;

GCC compiler will immediately report redefinition of the TypeDef ' X ', change g++ incredibly nothing, weird it. So to check the document, found in C + + is described in this way: "In a given non-class scope, a typedef specifier can is used to redefine the name of any type declare D in ' scope to ' refer to the ' type to which it already refers. "Well, it does look like C + + for non-class situation tolerance, as the following situation on the error:

Class a{

typedef int Y;

typedef int Y;

};

Unfortunately, in the C language standard find to find, did not see the redefinition of the clear description, not sure if the other C compiler will be an error.

I immediately thought of the macro, so I tried the following three kinds of case:

CASE1: #define M 1

#define M 1

CASE2: #define M (x) x + +

#define M (x) x + +

CASE3: #define M (x) (x+1)

#define M (x) (x + 1)

gcc/g++ compile (should say here is actually preprocessor Cc1/cc1plus) The result is consistent, CASE1/2 OK,CASE3 will error. Checked the criteria, that's how it is described.

(1) Tw o replacement lists are identical if and only if the preprocessing tokens in both have the same number, ordering, s Pelling, and white-space separation, where all white-space separations are considered.

(2) An identifier currently defined as a Object-like macro shall not being redefined by another #define preprocessing direct ive unless the second definition is a Object-like macro definition and the two replacement. Lists are. Likewise, a identifier currently defined as a Function-like macro shall not is redefined by another #define preprocessing directive unless the second definition is a Function-like macro definition, that has the same number and spelling of Param Eters, and the two replacement lists are

Identical.

The key point here is that only when the two words (exactly the same, including whitespace)

Http://hi.baidu.com/zhuxiaoyin/blog/item/f603f58e2e1321e2f11f3656.html

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.