473,405 Members | 2,421 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,405 software developers and data experts.

const callback

Hello all,

I'm in a process of writing a small Linux C++ program for discovering
repeaded files over a filesystem. One part of this task is traversing a
directory structure. This is done by the following recursive function.
Since I'd like it to be general enough to handle function pointers and
function objects I've made it a template.

template <typename CallBack>
void Traverse(const std::string& path, CallBack& callback) throw
(PosixError);

This brings an interesting problem: if I make second parameter is const
(and I'd like it to be that way) then:

1) G++ 3.3.6 refuses to compile the program
error: no matching function for call to `Traverse(char*&, void
(&)(const std::string&))'

2) G++ 4.1.1 compiles the program but the callback is never executed
(i.e. no output) if the callback is function. Works fine if given a
function objects.

Is it some kind of compliler issue or maybe a C++ mistake in my code?
TIA, Kyku

Here's the working part of the program:

#include <string>
#include <cstddef>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>

class PosixError : public std::exception
{
public:
PosixError() : _M_code(errno), _M_rep(strerror(_M_code)) {}
PosixError(int code) : _M_code(code), _M_rep(strerror(_M_code)) {}
int Code() const { return _M_code; }
const char* what() const throw() { return _M_rep.c_str(); }
~PosixError() throw() {}

private:
int _M_code;
std::string _M_rep;
};

class DirEntry
{
public:
const std::string& Name() { return _M_name; }
operator bool () const { return !_M_name.empty(); }

private:
DirEntry(struct dirent* dent) : _M_name(dent ? dent->d_name : "") {}

std::string _M_name;
friend class DirWalker;
};

class DirWalker
{
public:
DirWalker(const std::string& dirname) throw (PosixError) {
_M_dir = 0;
Open(dirname);
}

~DirWalker() { if (_M_dir != 0) closedir(_M_dir); }

operator bool () const { return _M_dir != 0; }

void Open(const std::string& dirname) throw (PosixError) {
Close();
_M_dir = opendir(dirname.c_str());
if (_M_dir == 0) throw PosixError();
}

void Close() {
if (_M_dir != 0) {
closedir(_M_dir);
_M_dir = 0;
}
}

DirEntry Read() throw(PosixError) {
const char* name;
struct dirent* dent;
do {
errno = 0;
dent = readdir(_M_dir);
if (errno != 0) throw PosixError();
name = dent->d_name;
} while (dent != 0 && name[0] == '.' && (name[1] == '\0' || name[1]
== '.' && name[2] == '\0'));
return DirEntry(dent);
}

private:
DIR* _M_dir;
};
template <typename CallBack>
void Traverse(const std::string& path, CallBack& callback) throw
(PosixError) {
DirWalker dw(path);
while (DirEntry de = dw.Read()) {
try {
struct stat buf;
std::string fullpath = path + '/' + de.Name();
if (stat(fullpath.c_str(), &buf) < 0) throw PosixError();
callback(fullpath);
if (S_ISDIR(buf.st_mode)) Traverse(fullpath, callback);
} catch (PosixError& pe) {
if (pe.Code() == EACCES) continue;
throw;
}
}
}

#include <iostream>

void PrintString(const std::string& name) { std::cout << name <<
std::endl; }

int main(int argc, char *argv[]) {
for (int i = 1; i < argc; ++i)
Traverse(argv[i], PrintString);
}

Aug 19 '06 #1
4 2796
Kyku wrote:
Is it some kind of compliler issue or maybe a C++ mistake in my code?
TIA, Kyku
I occurs to me that both behaviours are (different) compiler bugs,
though version 3.4.4 actually gets it right. :)

A look at the disassembly shows that 4.1 indeed doesn't emit any code
for the callback call if it's given by const reference.

I'm not an expert, but I don't think that's correct.

Jens
Aug 19 '06 #2

When I try this;

Traverse(argv[1],(void (*)(const std::string &)) PrintString);

Then it works... Interesting...

Tolga Ceylan

Aug 19 '06 #3
to***********@yahoo.com napisal(a):
When I try this;

Traverse(argv[1],(void (*)(const std::string &)) PrintString);

Then it works... Interesting...

Tolga Ceylan
I've recently found out that it also works if the function is preceded
by an ampersand:

Traverse(argv[i], &PrintString);

This compiles and works correctly in both v3 and v4. But then I always
thought that a function name on its own is equivalent to its address,
so these two invocations should be equivalent.

I've added the following line at the top of Traverse():

std::cout << __PRETTY_FUNCTION__ << std::endl;

The version with ampersand always (both g++3 and g++4) says:

void Traverse(const std::string&, const CallBack&) [with CallBack =
void (*)(const std::string&)]

the version without ampersand states (in g++4 ):

void Traverse(const std::string&, const CallBack&) [with CallBack =
void ()(const std::string&)]

What is "void ()(const std::string&)" is mistery to me (neither pointer
nor reference). Waiting for your input guys and gals.

Aug 20 '06 #4
Jens Theisen wrote:
Kyku wrote:
>Is it some kind of compliler issue or maybe a C++ mistake in my code?
TIA, Kyku

I occurs to me that both behaviours are (different) compiler bugs,
though version 3.4.4 actually gets it right. :)
This indeed is a bug in GCC. It is likely fixed now:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28385

Aug 20 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: scott ocamb | last post by:
hello I have implemented a solution using async methods. There is one async method that can be invoked multiple times, ie there are multiple async "threads" running at a time. When these...
19
by: Robert | last post by:
Greetings everyone, I was wondering if a const variable or object took up space. I know that a #define'd macro doesn't, as it's basically just interpreted by the compiler. If a const does take...
2
by: MR | last post by:
help! I have an unmanaged DLL that I do not have the source code, so i can't recompile or make changes. the DLL requires a callback function. I would like to implement the callback method in a...
5
by: Maxwell | last post by:
Hello, Newbie question here. I have a VS.NET 2003 MC++ (not C++/cli) project where I have a managed class reference in a unmanaged class...simple enough. To keep things short I am for the most...
5
by: Bit byte | last post by:
I have the following methods: static void Foo::setBar(const Bar*) ; //store a copy of Bar static const Bar* Foo::getBar(void) const ; //return an UNMODIFIABLE ptr to our internal copy In...
7
by: Kirk McDonald | last post by:
Let's say I have a function that takes a callback function as a parameter, and uses it to describe an iteration: def func(callback): for i in : callback(i) For the sake of argument, assume...
6
by: smmk25 | last post by:
Before I state the problem, I just want to let the readers know, I am knew to C++\CLI and interop so please forgive any newbie questions. I have a huge C library which I want to be able to use in...
10
by: SQACPP | last post by:
Hi, I try to figure out how to use Callback procedure in a C++ form project The following code *work* perfectly on a console project #include "Windows.h" BOOL CALLBACK...
0
by: Tim Spens | last post by:
--- On Fri, 6/27/08, Tim Spens <t_spens@yahoo.comwrote: I think I know where the problem is but I'm unsure how to fix it. When I call Register_Handler(...) from python via...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.