const-correctness and lambda expression
Hi, in my program, I have this function to compute the sum of sizes 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
Hi Matthias, On Apr 17, 2005, at 4:17 AM, Matthias Kaeppler wrote:
Hi,
in my program, I have this function to compute the sum of sizes 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.
You mean you are making get_total_file_size to be a const member function? Are m_first_file, and m_files members of DirBrowser? If so, this most likely makes *_1 (with a path object substituted for _1) return a const object. How does file_size take its argument? By non-const reference perhaps? If so, then that would be a reason. If this reasoning does not hold, can you provide some more context? Best, Jaakko Järvi
How come?
-- Matthias Kaeppler
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Jaakko Järvi, Assistant Professor -- Texas A&M University, Computer Science -- jarvi@cs.tamu.edu
participants (2)
-
Jaakko Järvi
-
Matthias Kaeppler