473,756 Members | 8,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(PosixErro r) {
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(fullpa th);
if (S_ISDIR(buf.st _mode)) Traverse(fullpa th, callback);
} catch (PosixError& pe) {
if (pe.Code() == EACCES) continue;
throw;
}
}
}

#include <iostream>

void PrintString(con st 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 2827
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***********@y ahoo.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_FUNCTI ON__ << 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
7139
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 threads are complete, the call the Callback method. Each "thread" calls the same callback method. What thread does this callback method exist on? My testing indicates the
19
16300
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 up space, is there any reason to choose it over a #define'd constant? -- Thank you P.S. if it makes any difference, I ssh to a SunOs machine where I use
2
1793
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 managed class. The unmanaged DLL gets the address of the callback in a struct (someStruct below) that is passed as a parameter. I can't seem to figure out how to pass the "unmanaged" address of a managed method to be uses as a callback. I would...
5
3417
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 part attempting to do what is this article by Nish: http://www.voidnish.com/articles/ShowArticle.aspx?code=cbwijw I have to hook up a unmanaged callback to a managed method using IJW NOT P\Invoke. So I am employing this "Thunk" or "Bridge" class...
5
1641
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 another part of my code , I retrieved and used Bar as follows: .... const Bar* temp = NULL ;
7
3596
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 the iteration is something more interesting than this which relies on the callback mechanism. The function is an existing interface, and I cannot change it.
6
7683
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 a .NET application and thus am looking into writing a managed C++ wrapper for in vs2005. Furthermore, this library has many callback hooks which need to be implemented by the C++ wrapper. These callback functions are declared as "extern C...
10
7000
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 MyEnumWindowsProc(HWND hwnd, LPARAM lparam) {
0
2114
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 callback.setHandler1(callback1) this only seems to affect pythons ability to trigger an "event" in c. PyObject *Handler is always NULL even after I call Register_Handler(...). I thought there was some magic here that was assigning the pointer *Handler to my python...
0
9462
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9287
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10046
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9886
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9722
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7259
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2677
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.