Last active
February 20, 2019 03:07
-
-
Save kulp/99c66d674e3c5931335cb15a041fb788 to your computer and use it in GitHub Desktop.
Simple traits demo for sub-arithmetic types
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
*.o | |
main |
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 "traits.hh" | |
#include <iostream> | |
typedef Thin< std::size_t > Bound; | |
int main() | |
{ | |
for (Bound thin(0); thin != Bound(10); ++thin) | |
{ | |
std::cout << "a line\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
CXXFLAGS += -Wall -Wextra -Werror -pedantic-errors | |
CXXFLAGS += -O3 | |
LINK.o = $(LINK.cc) | |
all: main | |
main.o: traits.hh | |
clean: | |
$(RM) main *.o |
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< typename Self, typename T, typename Base, T Base::* Member > | |
struct Preincrements | |
{ | |
Self & operator ++ () | |
{ | |
Self & self = static_cast<Self &>(*this); | |
++(self.*Member); | |
return self; | |
} | |
}; | |
template< typename Self, typename T, typename Base, T Base::* Member > | |
struct Eq | |
{ | |
bool operator == (Self const & that) | |
{ | |
Self & self = static_cast<Self &>(*this); | |
return self.*Member == that.*Member; | |
} | |
bool operator != (Self const & that) | |
{ | |
return !(*this == that); | |
} | |
}; | |
template< typename Self, typename T, typename Base, T Base::* Member > | |
struct PartialOrd | |
{ | |
bool operator < (Self const & that) | |
{ | |
Self & self = static_cast<Self &>(*this); | |
return self.*Member < that.*Member; | |
} | |
}; | |
template< typename T > | |
struct Base | |
{ | |
T member; | |
explicit Base(T const & in) | |
: member(in) | |
{ | |
// no body | |
} | |
}; | |
template< typename T > | |
class Thin | |
: public Base< T > | |
, public Preincrements< Thin< T >, T, Base< T >, &Base< T >::member > | |
, public Eq< Thin< T >, T, Base< T >, &Base< T >::member > | |
, public PartialOrd< Thin< T >, T, Base< T >, &Base< T >::member > | |
{ | |
private: | |
// Privatize a member inherited publicly | |
// Public inheritance simplifies the trait code significantly | |
using Base< T >::member; | |
public: | |
explicit Thin(T const & in) | |
: Base< T >(in) | |
{ | |
// no body | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment