Menu

#103 Support C++11 loop goodness

Next_Release
open
nobody
None
1
2016-11-10
2016-11-08
No

Since g++ now enables C++11 by default, we could consider altering IT++ objects to support the loop niceties, such as this:

std::vector<int> intvec = {9, 3, 2};

for (auto i : intvec)
    std::cout << i << ' ';

I don't know if such a change would be considered invasive, but if it is just about adding iterators, we could consider using it.

Discussion

  • Bogdan Cristea

    Bogdan Cristea - 2016-11-08

    Hi
    We should update the IT++ code to C++11. This task might require a moderate rewrite of the entire library, but I cannot be very sure. Could you come up with some design proposals and some examples of the modifications that should be made. Then we can split this rewrite in several tasks in order to allow several devs to work on this.
    regards
    Bogdan

     

    Last edit: Bogdan Cristea 2016-11-08
    • Kumar Appaiah

      Kumar Appaiah - 2016-11-09

      I will look into this and put my comments here. I agree with your assessment, and we should settle on a design and put the code together.

       
  • Kumar Appaiah

    Kumar Appaiah - 2016-11-09

    How is this for a start?

    #include <itpp/itcomm.h>
    #include <iostream>
    
    namespace itpp {
        double* begin(vec &v) {
            return &v[0];
        }
    
        double* end(vec &v) {
            return &v[v.size()];
        }
    };
    
    int
    main(int argc, char *argv[])
    {
        itpp::vec v = "3.0 3.3 -2.4";
        for (auto i : v) {
            std::cout << i << "\n";
        }       
        return 0;
    }
    

    Since this only involves adding a begin and end function, we could just provide a templatized version. This also doesn't affect compilation with older compilers.

    Please let me know your opinions.

     
  • Kumar Appaiah

    Kumar Appaiah - 2016-11-10

    Some more:

    #include <itpp/itcomm.h>
    #include <iostream>
    #include <algorithm>
    
    namespace itpp {
        template<class Num_T> Num_T* begin(Vec<Num_T> &v) {
            return &v[0];
        }
    
        template<class Num_T> Num_T* end(Vec<Num_T> &v) {
            return &v[v.size()];
        }
    };
    
    int
    main(int argc, char *argv[])
    {
        itpp::vec v = "3.0 3.3 -2.4";
        std::cout << "Vector:\n";
        for (auto i : v) {
            std::cout << i << "\n";
        }       
    
        itpp::bvec bv = "1 0 0 1";
        std::cout << "Binary vector:\n";
        for (auto i : bv) {
            std::cout << i << "\n";
        }       
    
        itpp::ivec iv = "-1 2 -3 4 -10";
        std::cout << "Integer vector:\n";
        for (auto i : iv) {
            std::cout << i << "\n";
        }       
    
        std::cout << "Integer vector doubled:\n";
        std::for_each(itpp::begin(iv), itpp::end(iv), [](int i) {
                std::cout << 2 * i << "\n";
            });
    
        std::cout << "How many negative numbers in integer vector?: " <<
        std::count_if(itpp::begin(iv), itpp::end(iv), [](int i) {
                return i < 0;
            }) << "\n";
    
        std::cout << "Sorting\n";
        std::sort(itpp::begin(iv), itpp::end(iv));
        std::cout << iv << "\n";
        return 0;
    }
    

    These examples don't really provide new features, since the above operations can likely be done using plain-old IT++ operator overloaded versions anyway. I'll try to think of some more useful ideas and post them here.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.