C++0x std::common_type

に std::common_type というメタ関数が入る予定

template <class ...T>
struct common_type;

template <class T>
struct common_type<T> {
    typedef T type;
};

template <class T, class U>
struct common_type<T, U> {
private:
    static T&& t();
    static U&& u();
public:
    typedef decltype(true ? t() : u()) type;
};

template <class T, class U, class ...V>
struct common_type<T, U, V...> {
    typedef typename common_type<typename common_type<T, U>::type, V...>::type type;
};

std::common_type は、テンプレートパラメータの型全てが暗黙に変換される型を返す

#include <type_traits>

using namespace std;

int main()
{
    static_assert(is_same<common_type<int, char, long>::type, long>::value, "not same"); // OK long

    return 0;
}


common_type は、D言語の標準ライブラリ(std.traits)ですでに実装されている



N2661 A Foundation to Sleep On

C++0x言語拡張まとめ