LoginSignup
2
2

More than 5 years have passed since last update.

C++ TMPでズンドコ関数

Last updated at Posted at 2016-03-23

若干今更感があるけれど噂のズンドコ関数をTMPで実装してみた

コード

#include<cstdint>
#include<type_traits>
#include<iostream>

template<std::uint32_t n>
struct seed_impl
{
    static const std::uint32_t value = (__TIME__ __DATE__)[n] + seed_impl<n-1>::value;
};
template<>
struct seed_impl<0>
{
    static const std::uint32_t value = (__TIME__ __DATE__)[0];
};
using seed= std::integral_constant<std::uint32_t, seed_impl<sizeof(__TIME__ __DATE__)-2>::value>;

template<std::uint32_t n>
class xorshift
{
    static const std::uint32_t a = n ^ (n << 13);
    static const std::uint32_t b = a ^ (a >> 17);
public:
    static const std::uint32_t value = b ^ (b << 5);
    using next = xorshift<value>;
};
void print(){}
template<class H,class ...T>
void print(H h,T...t)
{
    print(t...);
    if(h)
        std::cout<<"ズン";
    else
        std::cout<<"ドコ";
}

template<class R,class...T>
void zundoko(R,std::false_type ,std::true_type,std::true_type,std::true_type,std::true_type,T...x)
{
    print(std::false_type{}, std::true_type{},std::true_type{},std::true_type{},std::true_type{},x...);
    std::cout<<"キヨシ";
}
template<class R,class...T>
void zundoko(R,T...x)
{
    zundoko(typename R::next{}, std::integral_constant<bool, R::value % 2>{},x...);
}
#include<iostream>
int main()
{
    zundoko(xorshift<seed::value>{});
}

結果

Start
ズンズンドコドコドコズンズンドコドコドコズンドコドコドコドコズンドコドコドコズンズンズンドコズンズンズンズンズンズンドコキヨシ
0
Finish

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2