Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Alternative implementation of std::ptr_fun

$
0
0
Yo people!

I'm playing around with the Visual Studio C++ Nov CTP compiler and came up with an interesting implementation of what ended up being std::ptr_fun (Still working on the return type), but with no need to declare template arguments explicitly. What's your opinion about this implementation?

#include <utility>
#include <iostream>
#include <string>
#include <functional>

class C {
	void* f;
public:
	C(void* g) : f(g) { }

	template<typename... Args>
	void operator()(Args&&... args) {
		typedef void (*F)(Args...);
                //Perfect forwarding to f
		((F)f)(std::forward<Args>(args)...);
	}
};

void g(int i, const std::string& s) {
	std::cout << i << ' ' << s << std::endl;
}

int main() {
	std::string s = "abc";

	//No template stuff
	C c = g;
	c(2, s);

	//Ugly template stuff
	std::pointer_to_binary_function<int, const std::string&, void> f = std::ptr_fun(g);
	f(2, s);

	return 0;
}



Edit: I do know about auto, but let's forget about that for a sec!

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>