にゃははー

はへらー

型と値の共存

んーprogrammer's highというかなんか変なの考えたのでとりあえずうpしてみるけどこんなん使うやついるかねw

#include <typeinfo>

struct _type_or_value_impl
{
    typedef _type_or_value_impl this_type;

    virtual
    ~_type_or_value_impl( void ) noexcept {}
    virtual auto
    operator==( const this_type & ) const noexcept
      -> bool = 0;
    inline auto
    operator!=( const this_type &_tov ) const noexcept
      -> bool
    { return !( *this == _tov ); }
};

template < typename T, T... >
struct _type_or_value;

template < typename T >
struct _type_or_value< T >
  : public _type_or_value_impl
{
    typedef _type_or_value_impl __base;
    typedef _type_or_value< T > this_type;

    inline auto
    operator==( const __base &_tov ) const noexcept
      -> bool
    {
        try
        { dynamic_cast< const this_type & >( _tov ); }
        catch ( std::bad_cast & )
        { return false; }
        return true;
    }
};

template < typename T, T value >
struct _type_or_value< T, value >
  : public _type_or_value_impl
{
    typedef _type_or_value_impl __base;
    typedef _type_or_value< T, value > this_type;

    inline auto
    operator==( const __base &_tov ) const noexcept
      -> bool
    {
        try
        { dynamic_cast< const this_type & >( _tov ); }
        catch ( std::bad_cast & )
        { return false; }
        return true;
    }
};

#include <iostream>

auto
main( void )
  -> int
{
    auto bout = []( bool b )
    { std::cout << std::boolalpha << b << std::endl; };

    bout( _type_or_value< int >() == _type_or_value< int >() );
    bout( _type_or_value< int, 0 >() == _type_or_value< int, 0 >() );
    bout( _type_or_value< int >() != _type_or_value< int, 0 >() );
    bout( _type_or_value< int, 0 >() != _type_or_value< int >() );
    bout( _type_or_value< char >() != _type_or_value< int >() );
    bout( _type_or_value< char, 0 >() != _type_or_value< int, 0 >() );
    return 0;
}

こんなこと考えてコード書くなんて... 後悔はしてい...見なかったことにしよう