SERVING THE QUANTITATIVE FINANCE COMMUNITY

 
User avatar
Cuchulainn
Posts: 62591
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

C++ quiz --- STL tricks

September 9th, 2015, 3:11 pm

QuoteOriginally posted by: outrunThis might be relevant: from hereQuoteyou cannot use a function through async that expects non-const lvalue references as arguments, because async will always make a copy of them internally (or move them inside) to ensure they exist and are valid throughout the running time of the thread created.For sure. Josuttis page 959 "thou shall pass objects to async by value". So be it. It is a feature or a bug? So, we have 1. By value (OK)2. by & (meh)3. std::move() ... what does the standard say?
Last edited by Cuchulainn on September 8th, 2015, 10:00 pm, edited 1 time in total.
Step over the gap, not into it. Watch the space between platform and train.
http://www.datasimfinancial.com
http://www.datasim.nl
 
User avatar
Cuchulainn
Posts: 62591
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

C++ quiz --- STL tricks

September 9th, 2015, 4:28 pm

I tried some variations around the '&' theme (no async). The examples work, from which in this case it looks like reference captures is not advisable with async. I would not do it in general but that is not the issue.
Last edited by Cuchulainn on September 8th, 2015, 10:00 pm, edited 1 time in total.
Step over the gap, not into it. Watch the space between platform and train.
http://www.datasimfinancial.com
http://www.datasim.nl
 
User avatar
Cuchulainn
Posts: 62591
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

C++ quiz --- STL tricks

September 9th, 2015, 6:59 pm

QuoteOriginally posted by: outrunThat's indeed not the issue, we must understand why not (and in which case you still can)What I read here is that you *can* use ref in async, but you need to make sure that things don't go wrong.Yes, Josuttis. These are OK.It's the lambda case that causes the problems. Of course, C++14 init captures work as half-way house between = and &.
Last edited by Cuchulainn on September 8th, 2015, 10:00 pm, edited 1 time in total.
Step over the gap, not into it. Watch the space between platform and train.
http://www.datasimfinancial.com
http://www.datasim.nl
 
User avatar
Cuchulainn
Posts: 62591
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

C++ quiz --- STL tricks

September 9th, 2015, 7:26 pm

I am pretty sure I know what is up; in V1 'n' is call by value so it is an automatic/local so it goes out of scope so the lambda function just dies. Here is a solution that does not use call by value and it works:QuoteThe problem is that you'll immediately leave the CreateFuture after launching the thread and as soon as you leave, the reference to the "local n in CreateFuture" -of which a ref was captured inside the lambda- has gone out of scope and doesn't exist anymore.Yes.I agree with Item #31 in this case:
Last edited by Cuchulainn on September 8th, 2015, 10:00 pm, edited 1 time in total.
Step over the gap, not into it. Watch the space between platform and train.
http://www.datasimfinancial.com
http://www.datasim.nl
 
User avatar
Cuchulainn
Posts: 62591
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

C++ quiz --- STL tricks

September 9th, 2015, 7:33 pm

std::future<int> CreateFuture(int n){ // Create a future based on Rec recursion return std::async(std::launch::async, [&]() { return Rec(n); });}When this is called a copy of n is made as you say. Then it goes out of scope and Boom!
Last edited by Cuchulainn on September 8th, 2015, 10:00 pm, edited 1 time in total.
Step over the gap, not into it. Watch the space between platform and train.
http://www.datasimfinancial.com
http://www.datasim.nl
 
User avatar
Cuchulainn
Posts: 62591
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

C++ quiz --- STL tricks

September 9th, 2015, 8:19 pm

QuoteOriginally posted by: outrunYes, I think I now completely understand wat's going on! Nice puzzle and learning experience, thanks.You're welcome.
Step over the gap, not into it. Watch the space between platform and train.
http://www.datasimfinancial.com
http://www.datasim.nl
 
User avatar
Cuchulainn
Posts: 62591
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

C++ quiz --- STL tricks

September 10th, 2015, 2:36 pm

Ok, more low-level language quizzes. Here you go (multiple answers allowed). Answers before Saturday.QuoteQuiz Set 1 C++11/C++14// nullptr1. What is a nullptr? Give one answera) It is a null pointer constantb) It is the same as the C NULL macroc) It is a global C++ pointerd) It is an integer whose value is 02. How would you characterise nullptr? True or false?a) It resolves ambiguities inherent in C and C++b) It resolves the overloading pointer and integer syndrome in function callsc) std::nullptr_t converts to all pointer typesd) nullptr cannot be used with templates// static_assert3. What is static_assert?a) Is check that static variables have been initializedb) It is a compile-time check on the validity of a boolean conditionc) It is a run-time checkerd) Is it the same as the C assert macro4. Which of the following statements concerning static_assert is true?a) It leads to compile-time errorsb) It may appear at block scopec) It may not appear at class scoped) It only works with constant expressions and string literals// auto5. What is the auto specifier? a) It is similar to typedef to make code more readableb) It is used to declare variables and functions instead of fixed typesc) It is used to declare heterogeneous data types d) It is used to specialise template parameters6. Which of the following statements concerning the auto specifier is true?a) The type of variable being declared is automatically deduced from its initializerb) For functions, the return type is deduced from the return statementsc) The keyword auto may be accompanied by modifiers such as const and &d) Mixing auto variables and functions in one declarations is allowed// decltype7. What is decltype?a) It replaces typedef from C++11 onwardsb) It is used to let the compiler find the type of an expression or of an entityc) It is used to find the type of an expression at run-timed) It is used to convert values in an expression8. Which of the following statements concerning the decltype specifier is true?a) It can be used instead of an auto variableb) It can be used to determine the return type of template functionsc) It can be used when standard notation failsd) It cannot be used with lambda functions9. What is declval?a) It declares a reference typeb) It converts any type T to a reference typec) It generates values as input arguments to functionsd) It returns the return type of a function call expression at compile time// noexcept10. What is the noexcept specifier?a) It performs a compile-time check that returns true if an expression is declared to not throw any exceptionb) It specifies whether a function will throw exceptionsc) It is a way to suppress exceptions being thrown to clientsd) It ensures that all exceptions will be thrown from a function11. Which of the following statements regarding the noexcept specifier are true?a) If used, it guarantees that client functions will not throw exceptionsb) The C++98 exception specification is still supported in C++11 but it is deprecatedc) The stack is unwound when using the C++11 exception specificationd) It is part of a function?s specification12. What is the noexcept operator?a) It performs a compile-time check that returns true if an expression is declared to not throw any exceptionb) It serves the same objectives as the noexcept specifierc) It is not supported in C++11d) It specifies whether a function will throw exceptions// constexpr13. What is the constexpr specifier?a) It defines an expression that can be evaluated at compile timeb) It specifies that the value of a variable can appear in constant expressionsc) It specifies that the value of a variable or function can appear in constant expressionsd) It has the same functionality as const14. Which of the following statements regarding the constexpr specifier are true?a) All constexpr objects are const but not all const objects are constexpr b) A constexpr variable must be immediately constructed and assigned a valuec) constexpr functions may be virtuald) constexpr values are known at compile-time while const values can be defined at run-time// alias template 15. What is the alias template (type alias) declaration in C++11?a) It is a way to create an alias that can be used anywhere instead of a (possibly complex) non-template type nameb) It is a derived class of a template classc) It is a name that refers to a previously defined type (it refers to a family of types)d) It is used to improve code performance16. Which of the following statements regarding the alias template (type alias) declaration are true?a) It can be partially and explicitly specialisedb) It does not introduce a new typec) An alias template is defined using the keyword aliasd) It helps in making template code more readable (for example, C++ smart pointers)17. Which of the following statements regarding the alias template (type alias) declaration are true?a) An alias template cannot be defined in terms of another alias templateb) An alias can be used instead of the C++98 typedef declaration for both template and non-template synonymsc) The using keyword simplifies the readability of function pointer declarationsd) Aliased template and non-template classes can be members of classes (Composition)// New fundamental data types18. Which of the following new data types are supported in C++11?a) char32_tb) unsigned long longc) long intd) std::nullptr_t19. What is correct regarding main() in the C++ standard?a) It is require to provide a return statementb) Any return value other than 0 represents failurec) It may have void as its return typed) Only two declarations of main() are portable20. Which of the following regarding auto() are true?a) auto variables must be initialisedb) It is subject to pitfalls (for example, it can deduce undesired types)c) It is not immune to type mismatchesd) Mixing auto variables and functions in one declaration is allowed.//
Last edited by Cuchulainn on September 9th, 2015, 10:00 pm, edited 1 time in total.
Step over the gap, not into it. Watch the space between platform and train.
http://www.datasimfinancial.com
http://www.datasim.nl
 
User avatar
Cuchulainn
Posts: 62591
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

C++ quiz --- STL tricks

September 13th, 2015, 9:17 am

Instead of fancy futures, one can just use pthreads in C to compute. QED.It has much more functionality than C++ Concurrency.Fibonacci (1) is 1Fibonacci (3) is 2Fibonacci (5) is 5Fibonacci (7) is 13Fibonacci (10) is 55Fibonacci (15) is 610Fibonacci (21) is 10946Fibonacci (22) is 17711
Last edited by Cuchulainn on September 12th, 2015, 10:00 pm, edited 1 time in total.
Step over the gap, not into it. Watch the space between platform and train.
http://www.datasimfinancial.com
http://www.datasim.nl
 
User avatar
dd3
Posts: 246
Joined: June 8th, 2010, 9:02 am

C++ quiz --- STL tricks

September 14th, 2015, 12:47 pm

What's the point of that? What's wrong with std::thread?A lot of STL/Boost classes are nothing more thatn wrappers over OS provided functionality anyway.
 
User avatar
Cuchulainn
Posts: 62591
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

C++ quiz --- STL tricks

September 14th, 2015, 12:54 pm

QuoteOriginally posted by: dd3What's the point of that? What's wrong with std::thread?A lot of STL/Boost classes are nothing more thatn wrappers over OS provided functionality anyway.How can you schedule std::thread or give it a priority.
Step over the gap, not into it. Watch the space between platform and train.
http://www.datasimfinancial.com
http://www.datasim.nl
ABOUT WILMOTT

PW by JB

Wilmott.com has been "Serving the Quantitative Finance Community" since 2001. Continued...


Twitter LinkedIn Instagram

JOBS BOARD

JOBS BOARD

Looking for a quant job, risk, algo trading,...? Browse jobs here...


GZIP: On