By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,462 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

const-correctness and lambda expression

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Sat, 16 Apr 2005 23:46:10 +0200, Matthias Kaeppler
<no****@digitalraid.com> 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_si ze,*_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
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.