c++ - constexpr and endianness -


One common question from time to time in the world of C ++ programming is interval compilation time scheduling. Usually it is made with hard portable #ifdefs. But does the C ++ 11 constexpr keyword get better solution with the template specialization?

Will there be a legal C ++ 11 to do something like this:

  constexpr bool little_endian () {static unsigned number = 0xAABBCCDD; Return reinterpret_cast & lt; Constance unsigned char * & gt; (And number) [0] == 0xDD; }  

and then a template specialist for both endian types:

  template & lt; Bool LittleEndian & gt; Struct Foo {// .... expertise for a little endian}; Template & lt; & Gt; Foo & lt; False & gt; {// .... expertise for the big endian};  

And then:

  Foo < Little_endian () & gt; :: do_something ();  

Believing that the word is included, your example is bad (notice That there is no concept of "legal / illegal" in C ++). The proposed text for [Decl.constexpr] / 3 says that

  • Its work-body form will have a magnitude-statement {return expression; } where the expression is a potentially stable expression (5.1 9);

Your function violates that requirement, in which it also declares a local variable.

Edit : This restriction can be overcome. The function is still not well formed by moving num outside the function, therefore, because expression is a potentially stable expression , Which is defined as

An expression is a constant continuous expression if it is a continuous expression when all the occurrences of the function parameter are of appropriate type Will be replaced by continuous constant expression.

IOW, reinterpret_cast & lt; Constant unsigned characters * & gt; (& Amp; num) [0] == 0xDD must be a continuous expression, however, it is not: and; Num An address will be static-expression (5.1 9/4) receiving the value of such an indicator, however, it is not allowed for continuous expression:

Subscripting operator [] and class Members access and operator, and and * unary operators, and the indicator stop (except dynamic_ casts, 5.2.7) can be used to create a steady constant expression , But the value of an object can not be reached by using these operators H

Edit : The above text is from C ++ 98. Obviously, C ++ 0x is more acceptable, which allows for continued expression. The expression in the expression is a true-to-rivalual transformation, which is prohibited from continuous expression, unless

it is applied to an effective integral type of Non-volatile construction refers to the initial variable or static data member with constant expression

It is not clear to me whether (& amp; nbsp;) [0] "a reference variable" , Or even if only one literal num "refers to such a variable" if (& amp; num) [0] refers to that variable, then It is not clear whether reinterpret_cast & lt; Const unsigned char * & gt; (& Amp; num) [0] still refers to "" " num .


Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -