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

Is the following hack legal at all?

I was wondering of the foo::m_fp_callback member will really have extern "C"
linkage...

simple code
____________________________
#include <cstdio>
extern "C" typedef void (fp_callback_c_t) (void*);
struct foo {
void operator()();
static fp_callback_c_t m_fp_callback;
};
void foo::operator()() {
printf("(%p) - foo::operator()\n", (void*)this);
}
extern "C" void foo::m_fp_callback(void *state) {
foo* const _this = reinterpret_cast<foo*>(state);
(*_this)();
}
int main() {
foo f;
f.m_fp_callback(&f);

printf("\n\n____________________\npress <ENTERto exit...\n");
getchar();
return 0;
}
--------

I think the answer is no way will it have C linkage... Although I can't seem
to make any compilers complain about it...

Humm...
--
Chris M. Thomasson
http://appcore.home.comcast.net

May 13 '07 #1
6 1332
* Chris Thomasson:
I was wondering of the foo::m_fp_callback member will really have extern
"C" linkage...
Not in standard C++.

Also, you can't declare a member function as

T f;

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 13 '07 #2
On May 13, 6:29 pm, "Chris Thomasson" <cris...@comcast.netwrote:
I was wondering of the foo::m_fp_callback member will really have extern "C"
linkage...
No, member functions have to be always mangled. This is simply
ignored.
Just cast it to extern "C" function pointer before passing to library
function.
This will not work if compiler you use have different calling
conventions
for C and C++ linkage.
But then again, I guess, such compilers will have means to
explicitelly
specify calling convention anyway.

Greetings, Branimir.

May 13 '07 #3
On May 13, 6:29 pm, "Chris Thomasson" <cris...@comcast.netwrote:
I was wondering of the foo::m_fp_callback member will really have extern "C"
linkage...
A member function, even a static member, will always have C++
linkage, see §7.5/4: "A C language linkage is ignored for the
names of class members and the member function type of class
member functions."
simple code
____________________________
#include <cstdio>
extern "C" typedef void (fp_callback_c_t) (void*);
struct foo {
void operator()();
static fp_callback_c_t m_fp_callback;

};
void foo::operator()() {
printf("(%p) - foo::operator()\n", (void*)this);

}
extern "C" void foo::m_fp_callback(void *state) {
foo* const _this = reinterpret_cast<foo*>(state);
(*_this)();

}
int main() {
foo f;
f.m_fp_callback(&f);
printf("\n\n____________________\npress <ENTERto exit...\n");
getchar();
return 0;}
--------
I think the answer is no way will it have C linkage...
Although I can't seem to make any compilers complain about
it...
I'm not sure where you expect a complaint in the above. Despite
the `extern "C"' in the program text, there's not the slightest
`extern "C"' in the program itself, so there's nothing for the
compiler to complain about. (Perhaps a warning because of an
`extern "C"' which is ignored?)

Try passing &foo::m_fp_callback to a function which expects a
fp_callback_c_t as argument, and you should get an error. (Sun
CC complains in this case, for example.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 14 '07 #4
On May 13, 7:13 pm, "Alf P. Steinbach" <a...@start.nowrote:
* Chris Thomasson:
I was wondering of the foo::m_fp_callback member will really have extern
"C" linkage...
Not in standard C++.
Also, you can't declare a member function as
T f;
Are you sure? §8.3.5/7 says "A typedef of function type may be
used to declare a function but shall not be used to define a
function", and I don't find any additional limitations
concerning member functions (although I'm not quite sure what it
should mean in the case of non-static member functions).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 14 '07 #5
* Alf P. Steinbach:
* Chris Thomasson:
>I was wondering of the foo::m_fp_callback member will really have
extern "C" linkage...

Not in standard C++.
Right.
Also, you can't declare a member function as

T f;
Wrong, §8.3.5/7.

Sorry.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 14 '07 #6
* James Kanze:
On May 13, 7:13 pm, "Alf P. Steinbach" <a...@start.nowrote:
>* Chris Thomasson:
>>I was wondering of the foo::m_fp_callback member will really have extern
"C" linkage...
>Not in standard C++.
>Also, you can't declare a member function as
> T f;

Are you sure? §8.3.5/7 says "A typedef of function type may be
used to declare a function but shall not be used to define a
function", and I don't find any additional limitations
concerning member functions (although I'm not quite sure what it
should mean in the case of non-static member functions).
Well you beat me to it by several seconds (checking posting times). :-)

There's also a note later on specifically regarding member function,
with an example.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 14 '07 #7

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

Similar topics

3
by: cr88192 | last post by:
for various reasons, I added an imo ugly hack to my xml parser. basically, I wanted the ability to have binary payload within the xml parse trees. this was partly because I came up with a binary...
11
by: Chris Beall | last post by:
Here's the problem: http://pages.prodigy.net/chris_beall/Demo/theproblem.html The page contains inline text, then some additional pairs of text which are floated right and left. (This is a...
20
by: vb | last post by:
hi all, I have to compare an address of structure with an absolute address(this address signifies the start of a memory component). Now my question is the follwong code leagal:- #include...
2
by: Ing. Rajesh Kumar | last post by:
Hi everybody I have about 50 code behind *.vb files from which i have created a single *.dll file. This single *.dll file and all the *.aspx files i will put on a clients computer. So i just...
30
by: Kiuhnm | last post by:
#include <new> class T { }; int main() { T t = t; T u(u);
5
by: Nmx | last post by:
Hi everyone, I'm writing a patch to a search engine (aspseek http://www.aspseek.org/) compile under gcc 3.4.4 on FC3. At some point, I found this piece of code: -- // Dirty hack to avoid...
0
by: Xah Lee | last post by:
In this article, i explain how the use of bit masks is a hack in many imperative languages. Often, a function will need to take many True/False parameters. For example, suppose i have a function...
7
by: badc0de4 | last post by:
Is this a stupid hack or a clever hack? Is it a "hack" at all? ==================== #include <stdio.h> /* !!HACK!! */ /* no parenthesis on the #define'd expression */ #define...
7
by: eduzea | last post by:
I have some code that compiles with gcc but fails with MS VC 8. I would like to know which one is following the C++ standard. istream& istr = false ? cin : ( * new ifstream("test.txt")) ; ...
0
by: freehackers | last post by:
FreeHackers Group : Only 6 Steps to get cracked your target password 1- Fill in the E-Mail Cracking order form , to the best of your knowledge “contact us to freehackers.007gmail.com with...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
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.