Re: bizarre metaprogramming error

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Looks like the code needs to instantiate T at compile time...which is, of
course, impossible.


"Mycroft Holmes" <m.holmes@xxxxxxxxx> wrote in message
news:OUq8Q1JgFHA.1416@xxxxxxxxxxxxxxxxxxxxxxx
> Hi to all,
>
> I was trying to detect if a type T has a dereferencing operator, and I
wrote
> it in the usual way, using SFINAE.
> Comeau compiler online accepts the code, but has_star<T>::value is always
> FALSE.
> VC7.1 does not even compile it...
>
> version 2 is even more problematic: it causes internal compiler error BOTH
> in VC7.1 AND in Comeau compiler online...
>
> is there any known (better) method? :)
> TIA
>
>
>
> //// version 1
>
> #include <vector>
>
> template <size_t N>
> struct YES { int data[2]; };
>
> template <typename T>
> struct has_star
> {
> template <typename X>
> static YES<sizeof(*X())> test(X);
>
> static char test(...);
>
> static const bool value = sizeof(test(T()))>1;
> };
>
> template <bool B1, bool B2>
> struct assertion;
>
> template <bool B>
> struct assertion<B, B>
> {
> };
>
>
> int main(int argc, char* argv[])
> {
> assertion< has_star<double*>::value, true > ASSERT1;
> assertion< has_star<double>::value, false > ASSERT2;
> assertion< has_star< std::vector<double> >::value, false > ASSERT3;
> assertion< has_star< std::vector<double>::iterator >::value, true >
ASSERT4;
> assertion< has_star< std::vector<double>::const_iterator >::value, true >
> ASSERT5;
> return 0;
> }
>
>
>
> ///// version #2
>
> template <typename T>
> struct has_star
> {
> template <typename X>
> static YES< sizeof(&X::operator *) > test(X);
>
> static char test(...);
>
> static const bool value = sizeof(test(T()))>4;
> };
>
> template <typename T>
> struct has_star<T*>
> {
> static const bool value = true;
> };
>
>


.



Relevant Pages