久久久久久久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久久精品一区二区永久免费 | 久久久久久久久毛片 | 日韩av黄色 | 一区视频在线播放 | 免费观看一级毛片视频 | 高清18麻豆| 我爱操 | 日韩在线观看中文字幕 | 日韩欧美在 | 国产福利视频 | 国产乱码精品一区二区三区忘忧草 | 91在线一区二区三区 | 国产欧美在线 | 福利视频一区二区 | 91综合在线观看 | 韩国成人在线视频 | 久久久精品日本 | 免费一级黄色录像 | 日韩欧美一区在线 | 久久久国产精品 | 中文字幕二区 | 精品动漫一区 | 中文字幕av网址 | 国产一级在线视频 | 中文字幕亚洲一区二区三区 | 人人澡人人射 | 亚洲黄色国产 | 精品久久久久久18免费网站 | 国产区在线| 国产免费观看久久黄av片涩av | 色婷婷精品久久二区二区蜜臂av | 综合久久99| 国产wwwcom| 毛片免费视频 | 久精品久久 | 中文精品一区二区 | 亚洲美女网站 | 日韩在线观看中文字幕 | 国产精品久久久久久久免费大片 | 亚洲精品一区二区在线观看 | www.欧美|