(v) is actually (*&v) since when?

ぃ、小莉子 提交于 2019-12-04 16:09:10

问题


Could C++ standards gurus please enlighten me:

Since which C++ standard version has this statement failed because (v) seems to be equivalent to (*&v)?

I.e. for example the code:

 #define DEC(V) ( ((V)>0)? ((V)-=1) : 0 )
 ...{...
        register int v=1;
        int r = DEC(v) ;
 ...}...

This now produces warnings under -std=c++17 like:

cannot take address of register variable

left hand side of operand must be lvalue

Many C macros enclose ALL macro parameters in parentheses, of which the above is meant only to be a representative example.

The actual macros that produce warnings are for instance the RTA_* macros in/usr/include/linux/rtnetlink.h.

Short of not using/redefining these macros in C++, is there any workaround?


回答1:


If you look at the revision summary of the latest C++1z draft, you'd see this in [diff.cpp14.dcl.dcl]

[dcl.stc]
Change: Removal of register storage-class-specifier.
Rationale: Enable repurposing of deprecated keyword in future revisions of this International Standard.
Effect on original feature: A valid C++ 2014 declaration utilizing the register storage-class-specifier is ill-formed in this International Standard. The specifier can simply be removed to retain the original meaning.

The warning may be due to that.




回答2:


register is no longer a storage class specifier, you should remove it. Compilers may not be issuing the right error or warnings but your code should not have register to begin with

The following is a quote from the standard informing people about what they should do with regards to register in their code (relevant part emphasized), you probably have an old version of that file

C.1.6 Clause 10: declarations [diff.dcl]

Change: In C++, register is not a storage class specifier.

Rationale: The storage class specifier had no effect in C++. Effect on original feature: Deletion of semantically well-defined feature.

Difficulty of converting: Syntactic transformation.

How widely used: Common.




回答3:


Your worry is unwarranted since the file in question does not actually contain the register keyword:

grep "register" /usr/include/linux/rtnetlink.h

outputs nothing. Either way, you shouldn't be receiving the warning since:

  • System headers don't emit warnings by default, at least in GCC

  • It isn't wise to try to compile a file that belongs to a systems project like the linux kernel in C++ mode, as there may be subtle and nasty breaking changes

Just include the file normally or link the C code to your C++ binary. Report a bug if you really are getting a warning that should normally be suppressed to your compiler vendor.



来源:https://stackoverflow.com/questions/44693175/v-is-actually-v-since-when

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!