vc 鲜为人知的 __if_exists
2009-04-15 12:48
232 查看
msdn 中有这样一个示例:
但是不能检测某个变量是否有某个成员,象下面这样的代码是不能编译的:
或许是因为本质上,可以通过其它方法实现这个功能,也就是依据目前的这种机制,也可以达到目的,但是繁琐一些,需要加一个中间层,例如:
// the__if_exists_statement.cpp // compile with: /EHsc #include <iostream> template<typename T> class X : public T { public: void Dump() { std::cout << "In X<T>::Dump()" << std::endl; __if_exists(T::Dump) { T::Dump(); } __if_not_exists(T::Dump) { std::cout << "T::Dump does not exist" << std::endl; } } }; class A { public: void Dump() { std::cout << "In A::Dump()" << std::endl; } }; class B {}; bool g_bFlag = true; class C { public: void f(int); void f(double); }; int main() { X<A> x1; X<B> x2; x1.Dump(); x2.Dump(); __if_exists(::g_bFlag) { std::cout << "g_bFlag = " << g_bFlag << std::endl; } __if_exists(C::f) { std::cout << "C::f exists" << std::endl; } return 0; }
但是不能检测某个变量是否有某个成员,象下面这样的代码是不能编译的:
template<class TA> void foo(TA& a) { __if_exists(a.x) { printf("a has a.x/n"); } // 上面不能编译无所谓可以转化成 __if_exists(TA::x) { printf("a has a.x/n"); } // 但是这个就不是很容易了: __if_exists((complex expression).a) { } }
或许是因为本质上,可以通过其它方法实现这个功能,也就是依据目前的这种机制,也可以达到目的,但是繁琐一些,需要加一个中间层,例如:
template<class TA> void foo1(TA& a, true_type) { printf("a has a.x/n"); } template<class TA> void foo1(TA& a, false_type) { printf("a has not a.x/n"); } template<class TA> void foo(TA& a) { __if_exist(TA::x) { foo1(a, true_type()); } __if_not_exist(TA::x) { foo1(a, false_type()); } }
相关文章推荐
- vc 鲜为人知的 __if_exists
- C++编译期函数/变量检测技术,仿真VC关键字__if_exists
- C++编译期函数/变量检测技术,仿真VC关键字__if_exists
- IF Exists在Oracle中怎么用?
- vc中ifstream ofstream不能读取带有中文路径
- oracle 9i以上最有效率的if(exists)update else insert操作
- mysql insert if not exists防止插入重复记录的方法
- Oracle中没有 if exists(...)
- LoadRunner报这个错:invalid application path!please check if application exists
- mysql5.7基础 create database if not exists... 判断数据库是否存在 若不存在则创建
- # mysql CREATE TABLE IF NOT EXISTS metadata lock坑
- if exists和if not exists关键字用法
- freemarker中空值 null的处理 ?exists ?if_exists ?default(“”)
- freemarker中exists和if_exists区别及其序列空值的判断
- Oracle中没有 if exists(...)
- Oracle drop if exists
- How to do IF NOT EXISTS in SQLite
- mysql insert if not exists防止插入重复记录的方法
- boost一个编译时计数类的实现(__if_exists关键字)
- SqlServer IF Exists([database]|[table]|[prop]) / Column([Operation])