Example #1
0
int main()
{
    
    // not very related, but it's good to know other compile-time programming techniques
    // preprocessor macros have limited use, but they're still valid for a
    // number of programming patterns. 
    //
    // when running the compiler, preprocessor variables can be defined. there
    // variables can help to choose which code to compile. Useful for
    // multi-platform code, features that the user wants in the code...
#define ALTERNATIVE 5
#ifdef ALTERNATIVE
    std::cout << "yeah, compile-time programming existed before C++" << std::endl;
#else
    std::cout << "but not everything will be COMPILED" << std::endl;
#endif

#ifndef SOME_UNDEFINED_PREPROCESSOR_SYMBOL
    std::cout << "prints!" << std::endl;
#endif

#if ALTERNATIVE > 5
    std::cout << "comparisons are valid" << std::endl;
#endif


    // just to make it explicit...
    std::cout << "template adding with float: " << add((int)3, (int)5) << std::endl;

    // the function with templates accepts different types
    std::cout << "template adding with int: " << add((float)3.7, (float)15.0) << std::endl;

    int a = Const<9>::value;
    std::cout << "const: " << a << std::endl;

    int b = factorial<5>::value;
    std::cout << "factorial: " << b << std::endl;

    // traits

    int j = 9;
    //std::cout << "incremented: " << increment_type<decltype(j)>::increment_number(j) << std::endl;
    std::cout << "incremented: " << increment_number(j) << std::endl;

    // non-compiling template
    std::cout << increment_number('a') << std::endl;

    return 0;
}
Example #2
0
/* change_task: modifies the  integer number, and notes if the modified number
   is odd or even by setting the value of the common variable Even_Number */
void change_task(void) 
{
    /* increment value */
    const int inc_value = 1;

    int i; 

    while(1) 
    {
        /* change the number */
        for (i = 0; i < 1000; i++) 
        {
            increment_number(inc_value);
        }
        for (i = 0; i < 1000; i++) 
        {
            decrement_number(inc_value);
        }
    }
}