10/10/2004

  определения наличия функции-члена - (C++) 10:31 am

Взято как водится с RSDN, вот ссылка

#include <list>

namespace omni{

template <class T, T val>
struct member_wrapper{};

template <class T>
char test_for_swap(const T&, member_wrapper<void (T::*)(T&), &T::swap>* p = 0);

template <class T>
double test_for_swap(const T&, …);

template <class T>
struct has_member_swap
{
    static T t;
    static const bool value = (1 == sizeof(test_for_swap(t)));
};

//BOOST_STATIC_ASSERT(has_member_swap<std::list<int>::value);
//BOOST_STATIC_ASSERT(!has_member_swap<int>::value);
//BOOST_STATIC_ASSERT(!has_member_swap<noswap>::value);

template<bool b>
struct swapper
{
    template <class T>
    static void swap(T& t1, T& t2)
    { std::swap(t1, t2); }
};

template<>
struct swapper<true>
{
    template <class T>
    static void swap(T& t1, T& t2)
    { t1.swap(t2); }
};

template <class T>
void swap(T& t1, T& t2)
{
    swapper<has_member_swap<T>::value>::swap(t1, t2);
}

} // namespace omni

struct noswap{};

int main()
{
    std::list<intl1, l2;
    omni::swap(l1, l2);
    noswap s1, s2;
    omni::swap(s1, s2);
    int i1, i2;
    omni::swap(i1, i2);
    return 0;
}

Используется фича компиляторов “Substitution Failure Is Not An Error” (SFINAE). При разборе идеи следует обратить внимание на разницу в типах возвращаемых значений версий шаблона test_for_swap. Он тут самый главный.

9 Комментариев

  1. discount phentermine discount phentermine They that deny a God destroy man’s nobility; for certainly man is of kin to the beasts by his body; and, if

    Trackback by discount phentermine — 7/9/2005 @ 11:29 am

  2. poker games poker games Upon the whole, I am inclined to think that the far greater part, if not all, of those difficulties which have hitherto amused

    Trackback by poker games — 7/9/2005 @ 9:18 pm

  3. internet casino internet casino The nationalist not only does not disapprove of atrocities committed by his own side, but he has a remarkable capac

    Trackback by internet casino — 7/10/2005 @ 8:57 am

  4. cheap viagra cheap viagra Life is tragic simply because the earth turns and the sun inexorably rises and sets, and one day, for each of us, the sun will g

    Trackback by cheap viagra — 7/10/2005 @ 11:24 pm

  5. free online poker free online poker Injustice, poverty, slavery, ignorance - these may be cured by reform or revolution. But men do not live only by fig

    Trackback by free online poker — 7/15/2005 @ 6:35 pm

  6. gambling gambling This is a mistake which I seem to make eternally, that I imagine the sufferings of others as far greater than they really are. Ever si

    Trackback by gambling — 7/16/2005 @ 12:28 am

  7. diamonds diamonds Every law is an evil, for every law is an infraction of liberty.

    Trackback by diamonds — 9/4/2005 @ 1:43 am

  8. online poker online pok

    Trackback by online poker — 9/19/2005 @ 6:48 pm

  9. free polyphonic ringtones free polyphonic ringtones It sayis that a man escapes torn away from his scribe and commission’d out upon

    Trackback by free polyphonic ringtones — 10/6/2005 @ 2:25 am

Оставить комментарий

Комментарии к этой записи закрыты.