0
class A {};

class B {};

class C {};

class D {};

//A+B , A+C, B+C , A+D, D+C  namely all of these combinations will be possible just one functions 
4

1 に答える 1

2
template <typename T, typename U>
void foo(const T& pX, const U& pY)
{
    // do stuff
}

Tこれは、とのさまざまな組み合わせごとに新しい関数を作成するため、それ自体は望んでいるものではありませんが、U1 つの関数テンプレートです。


Tこれにより、 とUが同じになることが禁止されます。

template <bool> struct static_assert {};
template <> struct<true> static_assert {};

#define STATIC_ASSERT(pValue) static_assert<(pValue)>()

// ...

template <typename T, typename U>
struct is_different
{
    static const bool value = true;
};

template <typename T>
struct is_different<T, T>
{
    static const bool value = false;
};

// ...

template <typename T, typename U>
void foo(const T& pX, const U& pY)
{
    STATIC_ASSERT(is_different<T, U>::value);

    // do stuff
}
于 2010-06-05T22:10:43.273 に答える