C ++ 0xには、かなり包括的な包括的推論のサポートが追加されています。不必要な繰り返しを避けるために、可能な限りあらゆる場所で使用したいと思っていますが、あちこちで明示的な型情報を削除するのが良い考えかどうか疑問に思っています。このかなり不自然な例を考えてみましょう:
Foo.h
:
#include <set>
class Foo {
private:
static std::set<Foo*> instances;
public:
Foo();
~Foo();
// What does it return? Who cares! Just forward it!
static decltype(instances.begin()) begin() {
return instances.begin();
}
static decltype(instances.end()) end() {
return instances.end();
}
};
Foo.cpp
:
#include <Foo.h>
#include <Bar.h>
// The type need only be specified in one location!
// But I do have to open the header to find out what it actually is.
decltype(Foo::instances) Foo::instances;
Foo() {
// What is the type of x?
auto x = Bar::get_something();
// What does do_something() return?
auto y = x.do_something(*this);
// Well, it's convertible to bool somehow...
if (!y) throw "a constant, old school";
instances.insert(this);
}
~Foo() {
instances.erase(this);
}
これは妥当だと思いますか、それとも完全にばかげていますか?結局のところ、特に動的言語での開発に慣れている場合は、タイプのことをそれほど気にする必要はなく、コンパイラーがタイプシステムの悪質な悪用をキャッチすることを信頼できます。しかし、メソッドシグネチャのエディターサポートに依存している人にとっては、運が悪いので、ライブラリインターフェースでこのスタイルを使用することはおそらく非常に悪い習慣です。
可能なすべての型を暗黙的に記述することにより、コードの追跡が非常に簡単になることがわかります。これにより、C ++の通常の混乱がほぼすべて取り除かれます。もちろんあなたの走行距離は変わるかもしれません、そしてそれは私が聞いてみたいものです。型推論の根本的な使用に対する具体的な長所と短所は何ですか?