Last active
August 29, 2015 14:12
-
-
Save plasma-effect/839988dd1c9febbf6964 to your computer and use it in GitHub Desktop.
TMP_examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<class,class,class>struct torio; | |
template<class T>struct test; | |
template<class T1,class T2,class T3>struct test<torio<T1,T2,T3>> | |
{ | |
typedef T2 type; | |
}; | |
typedef torio<int,double,char> value_type; | |
int main() | |
{ | |
typename test<value_type>::type n; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<utility> | |
#include<array> | |
#include<iostream> | |
namespace test | |
{ | |
template<class ValueType, std::size_t Size>struct vector | |
{ | |
std::array<ValueType, Size> value; | |
template<class T>vector(T const& v) | |
{ | |
for (std::size_t i = 0; i<Size; ++i) | |
value[i] = v[i]; | |
} | |
ValueType const& operator[](std::size_t i)const | |
{ | |
return value[i]; | |
} | |
typedef ValueType value_type; | |
}; | |
template<class T0, class T1>struct add_vector | |
{ | |
T0 const& v0; | |
T1 const& v1; | |
add_vector(T0 const& a0, T1 const& a1) :v0(a0), v1(a1) {} | |
auto operator[](std::size_t i)const | |
{ | |
return v0[i] + v1[i]; | |
} | |
}; | |
template<class T0, class T1>add_vector<T0, T1> operator+(T0 const& a0, T1 const& a1) | |
{ | |
return add_vector<T0, T1>(a0, a1); | |
} | |
} | |
int main() | |
{ | |
typedef test::vector<int, 4> vec; | |
vec a{ std::array<int,4>{ { 1, 1, 1, 1 } } }; | |
vec b{ std::array<int,4>{ { 1, 2, 3, 4 } } }; | |
vec c{ std::array<int,4>{ { 1, 3, 7, 15} } }; | |
vec d{ a + b + c }; | |
for (std::size_t i = 0; i < 4; ++i) | |
{ | |
std::cout << d[i] << std::endl; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace test | |
{ | |
template<int N>struct get_arg | |
{ | |
template<class T,class... Ts>static constexpr auto const& get(T const&,Ts const&... args) | |
{ | |
return get_arg<N-1>::get(args...); | |
} | |
template<class... Ts>constexpr auto operator()(Ts const&... args)const | |
{ | |
return get(args...); | |
} | |
}; | |
template<>struct get_arg<0> | |
{ | |
template<class T,class... Ts>static constexpr auto const& get(T const& arg,Ts...) | |
{ | |
return arg; | |
} | |
template<class... Ts>constexpr auto operator()(Ts const&... args)const | |
{ | |
return get(args...); | |
} | |
}; | |
template<int N>constexpr get_arg<N> _{}; | |
template<class T0,class T1>struct operator_plus | |
{ | |
T0 v0; | |
T1 v1; | |
template<class... Ts>constexpr auto operator()(Ts const&... args)const | |
{ | |
return v0(args...) + v1(args...); | |
} | |
}; | |
template<class T0,class T1>constexpr operator_plus<T0,T1>operator+(T0 v0,T1 v1) | |
{ | |
return operator_plus<T0,T1>{v0, v1}; | |
} | |
} | |
int main() | |
{ | |
using namespace test; | |
constexpr auto add = _<0> + _<1>; | |
static_assert(add(1,2)==3,""); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<memory> | |
struct test | |
{ | |
struct inside_t | |
{ | |
virtual int run()=0; | |
virtual ~inside_t(){}; | |
}; | |
template<class T>struct inside:inside_t | |
{ | |
T value; | |
inside(T v):value(v){} | |
~inside(){} | |
virtual int run() | |
{ | |
return value.operator()(); | |
} | |
}; | |
std::unique_ptr<inside_t> holder_; | |
int operator()() | |
{ | |
return holder_->run(); | |
} | |
template<class T>test(T v):holder_(std::make_unique<inside<T>>(v)){} | |
~test()=default; | |
test(const test&)=delete; | |
}; | |
template<int N>struct hoge | |
{ | |
int operator()() | |
{ | |
return N; | |
} | |
}; | |
int main() | |
{ | |
test t{hoge<10>{}}; | |
test t2{hoge<20>{}}; | |
std::cout<<t()<<std::endl; | |
std::cout<<t2()<<std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment