Function Pointer as argument nightmare ('&' requires l-value)

Started by
22 comments, last by yacwroy 14 years, 7 months ago
Quote:Original post by Codeka
I also don't see the problem with recommending boost::function (or std::tr1::function) over a simple function pointer. Would you recommend someone use a simple char * until they actually "need" the functionality of std::string?

That is completely different. Character pointers are dangerous because it is easy to make mistakes when allocating and manipulating them.

Function pointers are relatively hard to mess up in comparison, unless you start trying to cast them.
Advertisement
Quote:Original post by swiftcoder
That is exactly my point - yacwroy is suggesting that everyone should know how to implement std::tr1::function before they use it, but the same does not hold for other elements of the standard library.


Aah, I see! At the time, it seemed straightforward that function pointers, as a core part of C++, be understood before using boost::function. I guess it would also be straightforward to use boost::function over function pointers if it was introduced first; just like std::string over char* as someone pointed out. Unfortunately, C++ is usually taught from the ground-up and function pointers, char* and typename* are encountered before std::tr1::function<>, std::string and auto<>...
Personally I don't think function pointers are the best solution. If I want typesafety then I use boost::function (or equivalent), if I want utter simplicity then I use a template parameter...

template<typename OnFinishFunction>void CSystem::PlayMovie(const char * filename, OnFinishFunction finish){	load_movie(filename).play();	finish();}
Quote:Original post by swiftcoder
Every programmer who uses std::map should know how to implement a balanced red-black tree too - but you don't see anyone recommending they learn that first, do you?
Quote:COME ON! I've used std::map and I don't know what a red-black tree is.
That is exactly my point -

yacwroy is suggesting that everyone should know how to implement std::tr1::function before they use it

No I am not.
If you read, I say they should know how to use function pointers. This does in no way imply they should know how to implement a function pointer wrapper class.
Using function pointers is only 1: putting a function's address into a variable, and 2: calling said function from a variable.


Quote:Original post by swiftcoder
The bigger issue I was hinting at, which you seem to have missed, is that nobody in this thread actually suggested Boost - let alone that you should rewrite your code to use boost::phoenix.
Quote:Really... nobody suggested boost in this thread? Have you read the thread?
Have you learned your standard library? In particular, go look in that <tr1/functional> header file I suggested. You will find std::tr1::function - otherwise known as boost::function. So no, nobody has suggested you use boost. Rather we have suggested that you use an element of the C++ standard library.

1. When a person says use boost::, they have suggested boost::. Even if it's in std::tr1, they said boost. And nobody here mentioned std::tr1 till you did.
2. std::tr1:: is not std::. It's not part of the standard library yet. It's just a set of candidates that may be added to std:: at some point in the future.
3. Unlikely as it seems, for all you know the std::tr1::function may undergo a syntactic change. Raw function pointers will not. boost:: may even be more concrete than std::tr1::.
Quote:Original post by wikipediaTR1 is not a standard itself, but rather a draft document. However, most of its proposals are likely to become part of the next official standard.



Quote:If someone had suggested something like boost::phoenix, I probably would have agreed with yacwroy, but for something as basic as boost::function, which has already been added (me: has not) to the C++ standard library, the anti-boost sentiment is ridiculous.

I think it's pretty unfair to call my comments anti-boost. If I say hammers are useful but you shouldn't go out and buy a hammer just to mash your potatoes or push in your doorstop, It's not anti-hammer sentiment.

This topic is closed to new replies.

Advertisement