views:

40

answers:

1
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 
+2  A: 
template <typename T, typename U>
void foo(const T& pX, const U& pY)
{
    // do stuff
}

This isn't what you want, per se, as it makes a new function for each different combination of T and U, but it is one function template.


This prohibits T and U from being the same:

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
}
GMan
If the OP only wants to write one function for all possible combinations, then this is the only way to go. However, it should also be possible to use polymorphism to write one addition function per class.
DeadMG
This includes A+A, B+B, B+A, etc. which are not supposed in the question.
Kirill V. Lyadvinsky
@Kirill: True, not too hard to stop though.
GMan