Skip to content

Instantly share code, notes, and snippets.

@Klaim
Created May 4, 2014 18:42
Show Gist options
  • Save Klaim/e9da89779310c7fa5ef8 to your computer and use it in GitHub Desktop.
Save Klaim/e9da89779310c7fa5ef8 to your computer and use it in GitHub Desktop.
#include <type_traits>
template< class ConditionType >
class Not
{
ConditionType m_condition;
public:
using this_type = Not<ConditionType>;
template< class ...Args >
Not( Args&&... args )
: m_condition( std::forward<Args>(args)... ) {}
Not( const Not& ) = default;
Not& operator=( const Not& ) = default;
template< class ...Args >
bool operator()( Args&&... args ) const
{
return ! m_condition( std::forward<Args>(args)... );
}
friend inline bool operator==( const Not& left, const Not& right )
{
return left.m_condition == right.m_condition;
}
friend inline bool operator<( const Not& left, const Not& right )
{
return left.m_condition < right.m_condition;
}
};
class ConditionThatCompile
{
int m_key;
public:
ConditionThatCompile( int key ) : m_key( key ) {}
bool operator()( const int& value ) const
{
return m_key > value;
}
friend inline bool operator==( const ConditionThatCompile& left, const ConditionThatCompile& right )
{
return left.m_key == right.m_key;
}
friend inline bool operator<( const ConditionThatCompile& left, const ConditionThatCompile& right )
{
return left.m_key < right.m_key;
}
};
class ConditionThatDoNotCompile
{
public:
bool operator()( const int& value ) const
{
return true;
}
friend inline bool operator==( const ConditionThatDoNotCompile& left, const ConditionThatDoNotCompile& right )
{
return true;
}
friend inline bool operator<( const ConditionThatDoNotCompile& left, const ConditionThatDoNotCompile& right )
{
return false;
}
};
void do_something();
void somewhere()
{
Not<ConditionThatCompile> compiling_condition { 42 };
auto other = compiling_condition; // error C2664: 'ConditionThatCompile::ConditionThatCompile(const ConditionThatCompile &)' : cannot convert argument 1 from 'Not<ConditionThatCompile>' to 'int'
if( compiling_condition( 100 ) )
do_something();
//Not<ConditionThatDoNotCompile> not_compiling_condition;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment