What is the deal with estimating the compilation time of constant arithmetic, and can this be done in the preprocessor?

template <int T>
void aFunc(){}

int main()
{

    int anArray[45-32];

    switch(4)
    {
    case 4<45:
        break;
    }
    aFunc<4*3/7&8 == 45 - 5>();
}

therefore, all this compiles in VC ++ 2005

- this standard? if so, what do conditional statements return? 0 and 1? are there any restrictions?

and what interests me the most is can you do this in macros? determines?

Edit:

to describe the preprocessor bit in more detail:

#define STRINGAFY(n)     #n
#define STRINGAFY_2(n)   STRINGAFY(n)

#define SOME_RANDOM_MACRO(n)     n


printf("%s", STRINGAFY(1));  //prints out "1"
printf("%s", STRINGAFY(SOME_RANDOM_MACRO(1)));  //prints out "SOME_RANDOM_MACRO(1)"
printf("%s", STRINGAFY_2(SOME_RANDOM_MACRO(1)));  //causes SOME_RANDOM_MACRO to be evaluated and prints out "1"
printf("%s", STRINGAFY_2(1+1)); // doesn't evaluate and print "2", prints "1+1" :(
+3
source share
5 answers

I think you misunderstood.

The actual evaluation of constant expressions is done by the compiler, not the preprocessor. The preprocessor evaluates only macros that relate to textual substitution.

Boost.Preprocessor, , , , if , .

BOOST_PP_ADD(4, 3) // expands to 7
BOOST_PP_SUB(4, 3) // expands to 1

, , ( ):

#define ADD_IMPL_4_3 7

#define BOOST_PP_ADD(lhs, rhs) ADD_IMPL_##lhs##_##rhs

, , ;)

, , .

template <int x> struct f {};

typedef f< 3*4 / 5 > super_f_type;

, ... !

: BOOST_PP_ADD , , > BOOST_PP_ADD(BOOST_PP_SUB(4,3),3).

+1

- ?

. " " . ( . 5.19 [expr.const] ++.)

, ? 0 1? ?

false true, 0 1. (, 0, & , ==.)

? ?

. #if, .

+7

++ Standard 5.19:

C + + , : (8.3.4, 5.3.4), (6.4.2), (9.6), (7.2), (9.4.2), (14.3).

constant-expression:  
            conditional-expression  

(2.13), , , (8.5), . (2.13.3) , . . , sizeof, , , .

(3.6.2).

+1

: , , .

, sizeof(int) . sizeof . #if sizeof(int)==4.

, .

+1

, , ++ bool, bool ( ++, C, - ). , . , ++.

0

Source: https://habr.com/ru/post/1736446/


All Articles