From: Nick Hounsome on 30 Apr 2010 02:18 On 29 Apr, 20:03, DeMarcus <use_my_alias_h...(a)hotmail.com> wrote: > Hi, > > In C++0x, is lambda the most proper way to initialize a std::function > with a valid function that does nothing? > > class SomeClass > { > public: > SomeClass() : fnc_([]{}) {} > > private: > std::function<void()> fnc_; > > }; Is it not a mistake to use lambdas in inline stuff for the same reasons that one would not put static void foo() {} in a header file? Apart from anything else moving the ctor impl to a cpp file would be less confusing. IMHO lambdas are just going to persuade more people that C++ is too complicated. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Daniel Krügler on 30 Apr 2010 02:25 On 30 Apr., 15:31, DeMarcus <use_my_alias_h...(a)hotmail.com> wrote: > Daniel Kr�gler wrote: > > On 29 Apr., 21:03, DeMarcus <use_my_alias_h...(a)hotmail.com> wrote: > > [..] > >> In C++0x, is lambda the most proper way to initialize a std::function > >> with a valid function that does nothing? > > >> class SomeClass > >> { > >> public: > >> SomeClass() : fnc_([]{}) {} > >> private: > >> std::function<void()> fnc_; > >> }; > > > I'm not sure whether I understand your question correctly: > > Is your requirement, that std::function should have a > > target? > > fnc_ may or may not be initialized with a proper function in a later > stage, i.e. > > void setFnc( std::function<void()> fnc ) > { > fnc_ = fnc; > } > > I just want to be able to run fnc_() without a crash if it hasn't been > initialized by the user. If you allow this assignment, you will probably need to verify that the provided std::function object does contain a target, otherwise you cannot give the guarantee you want to have, depending on what you precisely mean with "crash", see below. > > 1) If not, why is the default constructor not OK? > > Will the default constructor guarantee me that I can call fnc_ without a > crash? Depends on what you mean with "crash". std::function will throw std::bad_function_call, if a target-free object attempts to invoke the operator() overload. User code would need to check for a target before doing so. In fact, std::function is like a smart function pointer, therefore it provides a built-in null state. Personally, I never have seen the need to guarantee a non-null std::function object, because user code is aware of the optionality of the target (same as they are aware of the optionality within a general std::auto_ptr or boost::shared_ptr. This doesn't mean, that your idea is insane, it just depends on your particular use-case. HTH & Greetings from Bremen, Daniel Kr�gler -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Martin B. on 8 May 2010 02:24 On 30.04.2010 19:18, Nick Hounsome wrote: > On 29 Apr, 20:03, DeMarcus<use_my_alias_h...(a)hotmail.com> wrote: >> Hi, >> >> In C++0x, is lambda the most proper way to initialize a std::function >> with a valid function that does nothing? >> >> class SomeClass >> { >> public: >> SomeClass() : fnc_([]{}) {} >> >> private: >> std::function<void()> fnc_; >> >> }; > > Is it not a mistake to use lambdas in inline stuff for the same > reasons that one would not put > static void foo() {} > in a header file? > Why not? if it does_nothing, I don't care much where it's living, or do I? > Apart from anything else moving the ctor impl to a cpp file would be > less confusing. > I'd say it depends. If the ctor doesn't have any code but only a member initialization list, having it in the header (where the members are declared) might make the code more readable. > IMHO lambdas are just going to persuade more people that C++ is too > complicated. > Bah. What's complicated about an anonymous function? The syntax is something else though. Lamda's *might* persuade more people to think that the C++ language designers' idea of readable source code is slightly off. :-) cheers, Martin -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Mathias Gaunard on 9 May 2010 08:04 On 8 mai, 18:24, "Martin B." <0xCDCDC...(a)gmx.at> wrote: > > Is it not a mistake to use lambdas in inline stuff for the same > > reasons that one would not put > > static void foo() {} > > in a header file? > > Why not? if it does_nothing, I don't care much where it's living, or do I? Even if it does nothing it will generate some code, and I suspect the symbols will all have a unique name, preventing the linker from doing deduplication. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Martin B. on 10 May 2010 04:38 Mathias Gaunard wrote: > On 8 mai, 18:24, "Martin B." <0xCDCDC...(a)gmx.at> wrote: > >>> Is it not a mistake to use lambdas in inline stuff for the same >>> reasons that one would not put >>> static void foo() {} >>> in a header file? >> Why not? if it does_nothing, I don't care much where it's living, or do I? > > Even if it does nothing it will generate some code, and I suspect the > symbols will all have a unique name, preventing the linker from doing > deduplication. > As an aside to this: MSVC has a Linker Optimizer Setting "Enable COMDAT Folding" that will merge functions that generate identical code, so it doesn't matter what they're called. (This setting can create some confusion when debugging, though.) cheers, Martin -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: [ANN] CodeSynthesis XSD 3.3.0 released Next: ideas for data binding? |