C++に欲しい機能
C#書いていて、これC++にも欲しいなぁ、と思ったり、普通にこういう機能欲しいと思ったものです。
ネストした名前空間
今のC++ではこう書かないといけません。
namespace hoge { namespace foo { ... } }
をこう書けたら良いなぁ、と思いました。
namespace hoge::foo
{
...
}
enumの各種サポート関数
ユーザー定義属性(user defined attributes)
欲しいです。
ラムダ式の再帰
ラムダ式は関数オブジェクトなので、関数内部で、(*this)()
と書いて再帰できるようにして欲しいです。
struct factorial { int operator()(int n) { return n == 0 ? 1 : n * (*this)(n - 1); } }; #include <iostream> int main() { //こんなふうに書きたい //auto fact = [](int n){ return n == 0 ? 1 : n * (*this)(n - 1); }; auto fact = factorial(); std::cout << fact(5) << std::endl; }
調べたら、StackOverflowにこんなものが http://stackoverflow.com/questions/2067988/recursive-lambda-functions-in-c0x
ラムダ式で再帰が書けました。
#include <functional> #include <iostream> int main() { std::function<int (int)> fact; fact = [&fact](int n){ return n == 0 ? 1 : n * fact(n - 1); }; std::cout << fact(5) << std::endl; }