パスワードを忘れた? アカウント作成
91738 journal

eldeshの日記: 型計算で演算子オーバーロード(op[]編)

日記 by eldesh
書いてみた.
禁書目録インデックスを指定してリストから型を選択する.
やっぱりコンストラクタを呼び出すのが不格好だけど,
これはどうしようもなさげ.
型(の名前)にしか用がないのにConstructable(かな?)を要求することがそもそもイヤだけどw
でもとりあえず満足 :)

ところで,戻り型にautoを指定した関数ってどういうシグネチャ持つんだ?

#include <string>
#include <iostream>
#include <tuple>
#include <typeinfo>
#include <vector>

using namespace std;

template< int N >
struct Int {
    enum { value = N };
};

template< int N, typename HD, typename ...TL >
struct at {
};

template< int N, typename HD, typename ...TL >
struct at<N,tuple<HD,TL...>> {
    typedef typename at<N-1, tuple<TL...>>::type type;
};

template< typename HD, typename ...TL >
struct at<0,tuple<HD,TL...>> {
    typedef HD type;
};

template< typename ...Seq >
struct TypeVector {
    template< int N >
    auto operator[]( Int<N> ) -> typename at<N,tuple<Seq...>>::type
    { }
};

int main(){

    int v[] = { 1,2,3 };
    at<3,tuple<int,string,double,vector<int>>>::type x(v,v+3);
    cout << x[2] << endl;    // 3

    decltype(
            TypeVector<int,string,double,vector<int>>()[ Int<1>() ]
        ) str("hogehoge");
    cout << str << endl;    // hogehoge

    return 0;
}

この議論は賞味期限が切れたので、アーカイブ化されています。 新たにコメントを付けることはできません。
typodupeerror

普通のやつらの下を行け -- バッドノウハウ専門家

読み込み中...