473,385 Members | 1,324 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

const-correctness and lambda expression

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
1 2528
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Sergey Tolstov | last post by:
Hello, I am working with Visual C++ 6.0 compiler. In the following declaration: int const A = 10, B = 10; both A and B are const. However, in declaration
20
by: Corno | last post by:
Hi all, There's probably a good reason why a const object can call non const functions of the objects where it's member pointers point to. I just don't see it. For me, that makes the the const...
6
by: Virendra Verma | last post by:
This sounds weird, but I am looking for separate behaviors for destruction of a const and non-const object. I am trying to develop a smart/auto pointer class for writing objects to disk...
7
by: johny smith | last post by:
Can someone please explain to me the difference between these two: function1( const int a) function2( int const a) Both seemed to compile, but what is the difference between the two above....
3
by: Steven T. Hatton | last post by:
Sorry about the big code dump. I tried to get it down to the minimum required to demonstrate the problem. Although this is all done with GNU, I believe the problem I'm having may be more general. ...
15
by: Dave | last post by:
Hello NG, It is well known that memory-allocating definitions should not be put in a header file. I believe, however, that this does not apply to const definitions. For example: #ifndef...
4
by: chrisstankevitz | last post by:
This code does not compile on gcc 3.4.4. Should it? Thanks for your help, Chris //================ #include <set> int main()
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
17
by: Adrian Hawryluk | last post by:
Hi all, What is everyone's opinion of const inheriting? Should the object that a pointer is pointing at inherit the constness of the pointer? Such as in the case of a class having a pointer...
0
by: wellingj | last post by:
A little back ground on what I'm trying to do: I'm making a generic weighted graph class (vertexes and edges althought I don't call them that) to implement some pathfinding algorithms like A* and D*....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.