ä¸åç¹ã³ã³ããã¼ã¿ã§ C++14 ã® Generic Lambdas ãã¤ãã£ã¦ã¿ã
ãã£ãããªã®ã§æ¸ãã¦ã¿ã¾ããã
[ã½ã¼ã¹]
#include <iostream> // base code : http://d.hatena.ne.jp/iorate/20110729/1311949434 template<typename F> struct fix_result{ F f; template<typename ...Args> constexpr auto operator()(Args&&... args) const{ return f(*this, std::forward<Args>(args)...); } }; template<typename F> fix_result<typename std::decay<F>::type> fix(F&& f){ return { std::forward<F>(f) }; } int main(){ auto fact = [](auto f, auto n) // æ»ãå¤åãæ示ããªãã¨ã³ã³ãã¤ã©ãã¨ã©ã¼ã¯ãã -> decltype(n){ return n == 0 ? 1 : n * f(n - 1); }; auto f_fact = fix(fact); std::cout << f_fact(5) << std::endl; std::cout << fix([](auto f, auto n)->decltype(n){ return n <= 0 ? 1 : n + f(n - 1); })(100) << std::endl; return 0; }
[åºå]
120 5051
ã©ã ãã®æ»ãå¤åãæ示çã«è¨è¿°ããªãã¨ã¨ã©ã¼ã«ãªã£ãã®ã¯ã³ã³ãã¤ã©å´ã®ãã°ããªã
ã©ã ããããã ãã©é¢æ°ã®æ»ãå¤åã®åæ¨è«ãã ãã¶å¼·åã
[ã³ã³ãã¤ã©]
- clang++ (LLVM) 3.4 20131004(trunk)