久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C++:如何要求一種模板類型派生自另一種模板類型

C++: How to require that one template type is derived from the other(C++:如何要求一種模板類型派生自另一種模板類型)
本文介紹了C++:如何要求一種模板類型派生自另一種模板類型的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

在比較運算符中:

template<class R1, class R2>
bool operator==(Manager<R1> m1, Manager<R2> m2) {
    return m1.internal_field == m2.internal_field;
}

有什么辦法可以強制 R1 和 R2 必須具有超類型或子類型關系?也就是說,我想允許 R1 派生自 R2,或 R2 派生自 R1,但如果 R??1 和 R2 是不相關的類型,則不允許進行比較.

Is there any way I could enforce that R1 and R2 must have a supertype or subtype relation? That is, I'd like to allow either R1 to be derived from R2, or R2 to be derived from R1, but disallow the comparison if R1 and R2 are unrelated types.

推薦答案

您想要的特征可能如下所示:

A trait you want might look like this:

template <typename B, typename D>
struct is_base_of // check if B is a base of D
{
    typedef char yes[1];
    typedef char no[2];

    static yes& test(B*);
    static no& test(...);

    static D* get(void);

    static const bool value = sizeof(test(get()) == sizeof(yes);
};

那么你只需要某種靜態斷言:

Then you just need a static assert of some sort:

// really basic
template <bool>
struct static_assert;

template <>
struct static_assert<true> {}; // only true is defined

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

然后將兩者放在一起:

template<class R1, class R2>
bool operator==(Manager<R1> m1, Manager<R2> m2)
{
    STATIC_ASSERT(is_base_of<R1, R2>::value || is_base_of<R2, R1>::value);

    return p1.internal_field == p2.internal_field;
}

如果一個不派生自另一個,該函數將不會編譯.(您的錯誤將類似于static_assert not defined",它將指向該行.)

If one does not derive from the other, the function will not compile. (Your error will be similar to "static_assert<false> not defined", and it will point to that line.)

這篇關于C++:如何要求一種模板類型派生自另一種模板類型的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區別?)
Difference between const. pointer and reference?(常量之間的區別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 黄色三级在线播放 | 日韩欧美亚洲一区 | 日本在线精品视频 | 国产日产精品一区二区三区四区 | 午夜免费网站 | 亚洲综合中文字幕在线观看 | 二区三区av| 日韩免费网站 | 女女百合av大片一区二区三区九县 | 日本成人片在线观看 | 日韩精品免费一区 | 国产精品免费观看 | 久久精品国产亚洲a | 国产精品一级 | 天天天天天操 | 国外成人在线视频 | 成人欧美一区二区三区1314 | 亚洲69p | 国产污视频在线 | 国产精品观看 | 欧美伊人 | 国产精品一区二区在线观看 | 一级国产精品一级国产精品片 | 99reav| 成人av一区二区在线观看 | 午夜男人免费视频 | 久久精品国产v日韩v亚洲 | 国产在线观看一区二区 | 91在线资源| 一区二区三区视频在线免费观看 | 91精品国产乱码久久久久久久久 | 久久里面有精品 | 欧美一区二区三区精品 | 精品久久精品 | 国产免费看 | 成人国产精品久久久 | 日韩在线小视频 | 国产精品自产拍在线观看蜜 | 亚洲欧美在线视频 | 一区二区三区四区在线视频 | 成年人在线观看视频 |