eldeshの日記: 型計算で演算子オーバーロード(op[]編)
やっぱりコンストラクタを呼び出すのが不格好だけど,
これはどうしようもなさげ.
型(の名前)にしか用がないのに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;
}
型計算で演算子オーバーロード(op[]編) More ログイン