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

如何檢測類中是否存在特定的成員變量?

How to detect whether there is a specific member variable in class?(如何檢測類中是否存在特定的成員變量?)
本文介紹了如何檢測類中是否存在特定的成員變量?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

為了創建算法模板函數,我需要知道作為模板參數的類中是 x 還是 X(以及 y 或 Y).在將我的函數用于 MFC CPoint 類或 GDI+ PointF 類或其他一些類時,它可能很有用.他們都使用不同的 x .我的解決方案可以簡化為以下代碼:

For creating algorithm template function I need to know whether x or X (and y or Y) in class that is template argument. It may by useful when using my function for MFC CPoint class or GDI+ PointF class or some others. All of them use different x in them. My solution could be reduces to the following code:


template<int> struct TT {typedef int type;};
template<class P> bool Check_x(P p, typename TT<sizeof(&P::x)>::type b = 0) { return true; }
template<class P> bool Check_x(P p, typename TT<sizeof(&P::X)>::type b = 0) { return false; }

struct P1 {int x; };
struct P2 {float X; };
// it also could be struct P3 {unknown_type X; };

int main()
{
    P1 p1 = {1};
    P2 p2 = {1};

    Check_x(p1); // must return true
    Check_x(p2); // must return false

    return 0;
}

但它不能在 Visual Studio 中編譯,而在 GNU C++ 中編譯.使用 Visual Studio,我可以使用以下模板:

But it does not compile in Visual Studio, while compiling in the GNU C++. With Visual Studio I could use the following template:


template<class P> bool Check_x(P p, typename TT<&P::x==&P::x>::type b = 0) { return true; }
template<class P> bool Check_x(P p, typename TT<&P::X==&P::X>::type b = 0) { return false; }

但它不能在 GNU C++ 中編譯.有通用的解決方案嗎?

But it does not compile in GNU C++. Is there universal solution?

UPD:此處的結構 P1 和 P2 僅用作示例.可能有任何具有未知成員的類.

UPD: Structures P1 and P2 here are only for example. There are could be any classes with unknown members.

附言請不要在此處發布 C++11 解決方案,因為它們很明顯且與問題無關.

P.S. Please, do not post C++11 solutions here because they are obvious and not relevant to the question.

推薦答案

另一種方式是這個,它依賴于 SFINAE 表達式.如果名稱查找導致歧義,編譯器將拒絕模板

Another way is this one, which relies on SFINAE for expressions too. If the name lookup results in ambiguity, the compiler will reject the template

template<typename T> struct HasX { 
    struct Fallback { int x; }; // introduce member name "x"
    struct Derived : T, Fallback { };

    template<typename C, C> struct ChT; 

    template<typename C> static char (&f(ChT<int Fallback::*, &C::x>*))[1]; 
    template<typename C> static char (&f(...))[2]; 

    static bool const value = sizeof(f<Derived>(0)) == 2;
}; 

struct A { int x; };
struct B { int X; };

int main() { 
    std::cout << HasX<A>::value << std::endl; // 1
    std::cout << HasX<B>::value << std::endl; // 0
}

它基于 usenet 上某人的絕妙想法.

It's based on a brilliant idea of someone on usenet.

注意:HasX 檢查任何名為 x 的數據或函數成員,具有任意類型.引入成員名稱的唯一目的是使成員名稱查找可能存在歧義——成員的類型并不重要.

Note: HasX checks for any data or function member called x, with arbitrary type. The sole purpose of introducing the member name is to have a possible ambiguity for member-name lookup - the type of the member isn't important.

這篇關于如何檢測類中是否存在特定的成員變量?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How do I set the icon for my application in visual studio 2008?(如何在 Visual Studio 2008 中為我的應用程序設置圖標?)
Convert CString to const char*(將 CString 轉換為 const char*)
Remove secure warnings (_CRT_SECURE_NO_WARNINGS) from projects by default in Visual Studio(默認情況下,在 Visual Studio 中從項目中刪除安全警告 (_CRT_SECURE_NO_WARNINGS))
How do I start a new CUDA project in Visual Studio 2008?(如何在 Visual Studio 2008 中啟動新的 CUDA 項目?)
Exporting classes containing `std::` objects (vector, map etc.) from a DLL(從 DLL 導出包含 `std::` 對象(向量、映射等)的類)
What are some reasons a Release build would run differently than a Debug build(發布版本與調試版本的運行方式不同的一些原因是什么)
主站蜘蛛池模板: 黑人久久久 | 久久99久久99久久 | 欧美一级视频在线观看 | 成人在线视 | 国产美女在线看 | 久久综合一区二区三区 | 国产中文字幕在线观看 | 在线免费观看a级片 | 免费久久精品 | 成人精品免费视频 | 欧美一区二区三区高清视频 | 日本黄色片免费在线观看 | 综合在线视频 | 亚洲国产成人精品久久 | 欧美成人一区二免费视频软件 | www日本高清 | 美女操网站 | aaaaaa大片免费看最大的 | 国产三级国产精品 | 色在线免费| 一区二区三区福利视频 | 91中文视频 | 免费视频中文字幕 | 国产做a爱片久久毛片 | 一区二区三区精品在线视频 | 毛片在线看片 | 色综合久久天天综合网 | 91福利在线观看 | 9久久婷婷国产综合精品性色 | 中文av在线播放 | 精品国产一区二区国模嫣然 | 性色的免费视频 | 国产视频福利在线观看 | 免费看一区二区三区 | 欧美视频区 | 精品国产免费人成在线观看 | 亚洲国产精品一区二区第一页 | 在线观看av网站 | 成人无遮挡毛片免费看 | 亚洲成人综合网站 | 日韩欧美中文字幕在线观看 |