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
}

这本身并不是您想要的,因为它为Tand的每个不同组合创建了一个新函数U,但它是一个函数模板。


这禁止TU不一样:

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 回答