const-correctness and lambda expression

Discussion in 'C++' started by Matthias Kaeppler, Apr 16, 2005.

  1. Hi,

    in my program, I have this funtion to compute the sum of a bunch of files:

    Glib::ustring DirBrowser::get_total_file_size()
    {
    using namespace boostfs; // boostfs = boost::filesystem
    using namespace lambda; // lambda = boost::lambda
    boost::intmax_t sum = 0;
    std::for_each(m_first_file,m_files.end(),sum+=bind(&file_size,*_1));
    return make_size_human_readable( sum );
    }

    The line with for_each computes the sum of file sizes, by iterating over
    a container of pointers to boostfs::pathS, starting at the first file
    (there are also directories, before the files), till the end, invoking
    the lambda functor on each.

    Since this method does not change the state of the object, I wanted to
    declare it const, but suddenly the lambda expression starts complaining:
    Somehow, those path objects inside the lambda expression become const
    now, too, and it doesn't compile anymore.

    How come?

    --
    Matthias Kaeppler
     
    Matthias Kaeppler, Apr 16, 2005
    #1
    1. Advertising

  2. Matthias Kaeppler

    Kanenas Guest

    On Sat, 16 Apr 2005 23:46:10 +0200, Matthias Kaeppler
    <> wrote:

    >Hi,
    >
    >in my program, I have this funtion to compute the sum of a bunch of files:
    >
    >Glib::ustring DirBrowser::get_total_file_size()
    >{
    > using namespace boostfs; // boostfs = boost::filesystem
    > using namespace lambda; // lambda = boost::lambda
    > boost::intmax_t sum = 0;
    > std::for_each(m_first_file,m_files.end(),sum+=bind(&file_size,*_1));
    > return make_size_human_readable( sum );
    >}
    >

    [...]
    >
    >Since this method does not change the state of the object, I wanted to
    >declare it const, but suddenly the lambda expression starts complaining:
    >Somehow, those path objects inside the lambda expression become const
    >now, too, and it doesn't compile anymore.
    >
    >How come?


    Hmmm.

    I'm afraid without a little more info, there isn't much help we can
    offer. As the error arises from the use of a non-standard library, we
    can't know for certain what issue you are dealing with without the
    exact error messages (though I've a guess). We can't even know if
    it's a C++ issue or an issue with the library (in which case you'd
    need to ask the Boost mailing list or newsgroup). What is the exact
    text of the error messages? Is the error arising from the '*_1'
    expression, the 'bind' call or the '+=' call?

    I find it odd that the compiler complains about a const path, as
    file_size's declaration (for boost::filesystem v1.32.0) takes a const
    path&:
    boost::intmax_t file_size( const path & ph );
    Thus I'd expect that the bind would return a functor which (via
    template parameter deduction) would also take a const path&.

    My guess is the situation is similar to passing a const iterator, as
    opposed to some const_iterator type, to a function expecting a
    non-const iterator. As a shot in the dark, try:

    directory_iterator mff(m_first_file);
    std::for_each(mff,m_files.end(),sum+=bind(&file_size,*_1));

    If that works, explicit template parameters should also work, such as
    'for_each<directory_iterator>' or 'bind<boost::intmax_t, const path&,
    ???>' (I'm not certain of the desired type for '*_1').

    Does any of that come close?

    Kanenas
     
    Kanenas, Apr 20, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Strathmeyer

    stl list, const correctness

    Jim Strathmeyer, Mar 19, 2005, in forum: C++
    Replies:
    2
    Views:
    528
    Pete Becker
    Mar 20, 2005
  2. Javier
    Replies:
    2
    Views:
    621
    James Kanze
    Sep 4, 2007
  3. fungus
    Replies:
    13
    Views:
    946
    fungus
    Oct 31, 2008
  4. Francesco
    Replies:
    6
    Views:
    398
    Alf P. Steinbach
    Sep 3, 2009
  5. Veli-Pekka Tätilä
    Replies:
    2
    Views:
    133
    Eric J. Roode
    Aug 27, 2005
Loading...

Share This Page