const-correctness and lambda expression

M

Matthias Kaeppler

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?
 
K

Kanenas

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top