Crimes against C++

http://ideone.com/OnsDWr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

struct Extension
{
	void f(){std::cout<<"f"<<std::endl;}
	float g(int x, float y, int z)
	{
		std::cout << "x = " << x << std::endl
		          << "y = " << y << std::endl
		          << "z = " << z << std::endl;
		return 4.5f;
	}
};

template<typename R, typename... Args>
R CallExtMF(Extension &ext, void *f, Args... args)
{
	R (Extension::*mfp)(Args...) = *reinterpret_cast<R (Extension::**)(Args...)>(&f);
	return (ext.*mfp)(args...);
}

int main()
{
	void *f = reinterpret_cast<void *>(&Extension::f);
	void *g = reinterpret_cast<void *>(&Extension::g);
	
	Extension e;
	
	CallExtMF<void>(e, f);
	float p = 2.5f;
	float rv = CallExtMF<float>(e, g, 1, *reinterpret_cast<int *>(&p), 3);
	std::cout << "rv = " << rv << std::endl;
}
I need this for retrofitting a project - don't ask. Just thought I would horrify some of you.
Last edited on
This drove me mad the other day. It's basically an iterable static tuple:
http://www.cplusplus.com/forum/general/115963/#msg633095
http://www.cplusplus.com/forum/general/115963/#msg633118
I'm gagging and vomiting at the same time. I'm.. ga-vomiting.
> http://ideone.com/OnsDWr
I don't see the point in such link

To clarify: {clan,}g++ refuse to compile because of the function casting
http://www.parashift.com/c++-faq/cant-cvt-fnptr-to-voidptr.html

g++ complains with -pedantic-errors, doing it permissive produces
f
x = 1
y = 0
z = 1075838976
rv = 4.5
Last edited on
@ne555 I plead guilty, I don't need a trial.
Topic archived. No new replies allowed.