<分区>
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
<分区>
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
最佳答案
template <typename T, typename U>
void foo(const T& pX, const U& pY)
{
// do stuff
}
这本身并不是您想要的,因为它为 T
和 U
的每个不同组合创建一个新函数,但它是一个函数模板。
这禁止 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
}
关于c++ - 不同类如何操作一个函数一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2982294/