c++ - 不同类如何操作一个函数一个

标签 c++ class overloading operator-keyword

<分区>

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
}

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


这禁止 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
}

关于c++ - 不同类如何操作一个函数一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2982294/

相关文章:

c++ - 字符串重载运算符 ">>"

objective-c - Objective C 中是否可以实现函数重载?

c++ - new with inheritance 的不当使用?

php - 从存储在变量中的字符串中获取对象数据

c++ - 为 find_if 重载函数调用运算符 ()

Scala双重定义(2个方法具有相同的类型删除)

c++ - 错误 : no instance of overloaded function

c++ - 堆栈、高速缓存未命中和虚拟内存

c++ - 在 MFC 中使用指针和类创建无模式窗口的区别

c++ - 为什么第一 block 代码产生垃圾值,而第二 block 代码将类成员的值相加?