diff options
author | upstream source tree <ports@midipix.org> | 2015-03-15 20:14:05 -0400 |
---|---|---|
committer | upstream source tree <ports@midipix.org> | 2015-03-15 20:14:05 -0400 |
commit | 554fd8c5195424bdbcabf5de30fdc183aba391bd (patch) | |
tree | 976dc5ab7fddf506dadce60ae936f43f58787092 /gcc/testsuite/g++.dg/template/sfinae10.C | |
download | cbb-gcc-4.6.4-554fd8c5195424bdbcabf5de30fdc183aba391bd.tar.bz2 cbb-gcc-4.6.4-554fd8c5195424bdbcabf5de30fdc183aba391bd.tar.xz |
obtained gcc-4.6.4.tar.bz2 from upstream website;upstream
verified gcc-4.6.4.tar.bz2.sig;
imported gcc-4.6.4 source tree from verified upstream tarball.
downloading a git-generated archive based on the 'upstream' tag
should provide you with a source tree that is binary identical
to the one extracted from the above tarball.
if you have obtained the source via the command 'git clone',
however, do note that line-endings of files in your working
directory might differ from line-endings of the respective
files in the upstream repository.
Diffstat (limited to 'gcc/testsuite/g++.dg/template/sfinae10.C')
-rw-r--r-- | gcc/testsuite/g++.dg/template/sfinae10.C | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/gcc/testsuite/g++.dg/template/sfinae10.C b/gcc/testsuite/g++.dg/template/sfinae10.C new file mode 100644 index 000000000..f44c4458f --- /dev/null +++ b/gcc/testsuite/g++.dg/template/sfinae10.C @@ -0,0 +1,181 @@ +// DR 339 +// +// Test of the use of various unary operators with SFINAE + +// Boilerplate helpers +typedef char yes_type; +struct no_type { char data[2]; }; + +template<typename T> T create_a(); +template<typename T> struct type { }; + +template<bool, typename T = void> struct enable_if { typedef T type; }; +template<typename T> struct enable_if<false, T> { }; + +#define JOIN( X, Y ) DO_JOIN( X, Y ) +#define DO_JOIN( X, Y ) DO_JOIN2(X,Y) +#define DO_JOIN2( X, Y ) X##Y + +#define DEFINE_PREFIX_UNARY_TRAIT(Name,Op) \ +template<typename T> \ + typename enable_if<(sizeof(Op create_a<T>(), 1) > 0), \ + yes_type>::type \ + JOIN(check_,Name)(int); \ + \ +template<typename T> \ + no_type JOIN(check_,Name)(...); \ + \ +template<typename T> \ +struct Name \ +{ \ + static const bool value = \ + (sizeof(JOIN(check_,Name)<T&>(0)) == sizeof(yes_type)); \ +} + +#define DEFINE_POSTFIX_UNARY_TRAIT(Name,Op) \ +template<typename T> \ + typename enable_if<(sizeof(create_a<T>() Op, 1) > 0), \ + yes_type>::type \ + JOIN(check_,Name)(int); \ + \ +template<typename T> \ + no_type JOIN(check_,Name)(...); \ + \ +template<typename T> \ +struct Name \ +{ \ + static const bool value = \ + (sizeof(JOIN(check_,Name)<T&>(0)) == sizeof(yes_type)); \ +} + +#ifdef __GXX_EXPERIMENTAL_CXX0X__ +# define STATIC_ASSERT(Expr) static_assert(Expr, #Expr) +#else +# define STATIC_ASSERT(Expr) int JOIN(a,__LINE__)[Expr? 1 : -1] +#endif + +struct W { + W operator+(); + W operator-(); + int operator*(); + W operator~(); + bool operator!(); + W& operator++(); + W& operator--(); + W& operator++(int); + W& operator--(int); +}; + +struct X { }; +X operator+(X); +X operator-(X); +int operator*(X); +X operator~(X); +bool operator!(X); +X& operator++(X&); +X& operator--(X&); +X& operator++(X&, int); +X& operator--(X&, int); + +struct Y { }; + +struct Z { +private: + Z operator+(); // { dg-error "is private" } + Z operator-(); // { dg-error "is private" } + int operator*(); // { dg-error "is private" } + Z operator~(); // { dg-error "is private" } + bool operator!(); // { dg-error "is private" } + Z& operator++(); // { dg-error "is private" } + Z& operator--(); // { dg-error "is private" } + Z& operator++(int); // { dg-error "is private" } + Z& operator--(int); // { dg-error "is private" } +}; + +// has_unary_plus +DEFINE_PREFIX_UNARY_TRAIT(has_unary_plus, +); // { dg-error "within this context" } +STATIC_ASSERT((has_unary_plus<int>::value)); +STATIC_ASSERT((!has_unary_plus<int X::*>::value)); +STATIC_ASSERT((has_unary_plus<W>::value)); +STATIC_ASSERT((has_unary_plus<X>::value)); +STATIC_ASSERT((!has_unary_plus<Y>::value)); + +// is_negatable +DEFINE_PREFIX_UNARY_TRAIT(is_negatable, -); // { dg-error "within this context" } +STATIC_ASSERT((is_negatable<int>::value)); +STATIC_ASSERT((!is_negatable<int X::*>::value)); +STATIC_ASSERT((is_negatable<W>::value)); +STATIC_ASSERT((is_negatable<X>::value)); +STATIC_ASSERT((!is_negatable<Y>::value)); + +// is_dereferenceable +DEFINE_PREFIX_UNARY_TRAIT(is_dereferenceable, *); // { dg-error "within this context" } +STATIC_ASSERT((!is_dereferenceable<int>::value)); +STATIC_ASSERT((is_dereferenceable<int*>::value)); +STATIC_ASSERT((is_dereferenceable<W>::value)); +STATIC_ASSERT((is_dereferenceable<X>::value)); +STATIC_ASSERT((!is_dereferenceable<Y>::value)); + +// has_bitwise_not +DEFINE_PREFIX_UNARY_TRAIT(has_bitwise_not, ~); // { dg-error "within this context" } +STATIC_ASSERT((has_bitwise_not<int>::value)); +STATIC_ASSERT((!has_bitwise_not<int*>::value)); +STATIC_ASSERT((has_bitwise_not<W>::value)); +STATIC_ASSERT((has_bitwise_not<X>::value)); +STATIC_ASSERT((!has_bitwise_not<Y>::value)); + +// has_truth_not +DEFINE_PREFIX_UNARY_TRAIT(has_truth_not, !); // { dg-error "within this context" } +STATIC_ASSERT((has_truth_not<int>::value)); +STATIC_ASSERT((has_truth_not<int*>::value)); +STATIC_ASSERT((has_truth_not<W>::value)); +STATIC_ASSERT((has_truth_not<X>::value)); +STATIC_ASSERT((!has_truth_not<Y>::value)); + +// has_preincrement +DEFINE_PREFIX_UNARY_TRAIT(has_preincrement, ++); // { dg-error "within this context" } +STATIC_ASSERT((has_preincrement<int>::value)); +STATIC_ASSERT((has_preincrement<int*>::value)); +STATIC_ASSERT((!has_preincrement<int X::*>::value)); +STATIC_ASSERT((has_preincrement<W>::value)); +STATIC_ASSERT((has_preincrement<X>::value)); +STATIC_ASSERT((!has_preincrement<Y>::value)); + +// has_predecrement +DEFINE_PREFIX_UNARY_TRAIT(has_predecrement, --); // { dg-error "within this context" } +STATIC_ASSERT((has_predecrement<int>::value)); +STATIC_ASSERT((has_predecrement<int*>::value)); +STATIC_ASSERT((!has_predecrement<int X::*>::value)); +STATIC_ASSERT((has_predecrement<W>::value)); +STATIC_ASSERT((has_predecrement<X>::value)); +STATIC_ASSERT((!has_predecrement<Y>::value)); + +// has_postincrement +DEFINE_POSTFIX_UNARY_TRAIT(has_postincrement, ++); // { dg-error "within this context" } +STATIC_ASSERT((has_postincrement<int>::value)); +STATIC_ASSERT((has_postincrement<int*>::value)); +STATIC_ASSERT((!has_postincrement<int X::*>::value)); +STATIC_ASSERT((has_postincrement<W>::value)); +STATIC_ASSERT((has_postincrement<X>::value)); +STATIC_ASSERT((!has_postincrement<Y>::value)); + +// has_postdecrement +DEFINE_POSTFIX_UNARY_TRAIT(has_postdecrement, --); // { dg-error "within this context" } +STATIC_ASSERT((has_postdecrement<int>::value)); +STATIC_ASSERT((has_postdecrement<int*>::value)); +STATIC_ASSERT((!has_postdecrement<int X::*>::value)); +STATIC_ASSERT((has_postdecrement<W>::value)); +STATIC_ASSERT((has_postdecrement<X>::value)); +STATIC_ASSERT((!has_postdecrement<Y>::value)); + +// Check for private members +STATIC_ASSERT((has_unary_plus<Z>::value)); // { dg-message "instantiated from here" } +STATIC_ASSERT((is_negatable<Z>::value)); // { dg-message "instantiated from here" } +STATIC_ASSERT((is_dereferenceable<Z>::value)); // { dg-message "instantiated from here" } +STATIC_ASSERT((has_bitwise_not<Z>::value)); // { dg-message "instantiated from here" } +STATIC_ASSERT((has_truth_not<Z>::value)); // { dg-message "instantiated from here" } +STATIC_ASSERT((has_preincrement<Z>::value)); // { dg-message "instantiated from here" } +STATIC_ASSERT((has_predecrement<Z>::value)); // { dg-message "instantiated from here" } +STATIC_ASSERT((has_postincrement<Z>::value)); // { dg-message "instantiated from here" } +STATIC_ASSERT((has_postdecrement<Z>::value)); // { dg-message "instantiated from here" } + |