mardi 30 mai 2017

Using nested macro with different number of arguments in C++

The following code fails in compilation by g++ -std=c++11 compiler.

    # include<iostream>
    # include<vector>

    using namespace std;


    # define stlf(x)        x.begin(), x.end()
    # define repf(it, a, b) for(auto it = a ; it != b ; ++it)


    /*
    // Also, following alternative fails

    # define repf(it, a, b) for(auto it = a ; it != b ; ++it)
    # define stlf(x)        x.begin(), x.end()

    */



    typedef vector<int > vi;

    # define pd(x)  printf("%d", x);

    int main(void){

        vi arr(10, -1);

        repf(arr, stlf(arr))
            pd(arr[i]);


        return 0;
    }

1. Why is this happening ?

2. What could have been implementation problem for C++ Pre-Processor implementors, that they avoided this feature ?

3. How can I then use such shortcuts ??

Aucun commentaire:

Enregistrer un commentaire