作者:
rockeet
发表日期:
2009年04月18日
分类:
C++
评论:
0 条
阅读次数: 1,916 次
使用操作符重载时,出现模板匹配错误,bug 的出现很简单,下面是代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include <stdio.h> #include <string> struct A1 { template<class Ch, class Tr, class Al> void operator<<(std::basic_string<Ch, Tr, Al>& x) { printf("void operator<<(std::basic_string<Ch, Tr, Al>& x)/n"); } }; struct A2 { void operator<<(std::string& x) { printf("void operator<<(std::string& x)/n"); } }; struct B1 : A1 { using A1::operator<<; template<class T> void operator<<(T& x) { printf("void operator<<(T& x)/n"); } }; struct B2 : A2 { using A2::operator<<; template<class T> void operator<<(T& x) { printf("void operator<<(T& x)/n"); } }; int main(int argc, char* argv[]) { std::string s = "abc"; B1 b1; // 下面两行调用了不同的函数,为什么? // these two line call different function, why? b1 << s; b1.operator<<(s); printf("/n"); B2 b2; // OK, 完全正确 // OK, all right b2 << s; b2.operator<<(s); return 0; } |
作者:
rockeet
发表日期:
2009年04月18日
分类:
C++
评论:
0 条
阅读次数: 2,132 次
项目地址:http://code.google.com/p/febird
用gcc4.3重新编译了一下febird,出现了很多错误,仔细观察,这些错误都是因为不符合C++标准,重新改成符合标准的,比想象的改动量要大。
又测试了一下纯 C 实现的 algorithm: febird.c,是从VC2008的stl代码改过来的,在VC2008中测试比std::sort快20%,但是一到gcc中,却比std::sort慢了一倍还不止!不知到dinkware怎么写的。他有没有和sgi的比较过。重新研读了一下 gcc4.3 的 algorithm代码,gcc.stl.sort把insertion_sort放到sort的最后一个阶段,而ms.stl.sort把insertion_sort放到小区间内,就仅从这点上讲,gcc.stl.sort就少了很多函数调用的开销,ms又做了一些看似聪明的优化,比如递归层次按log(1.5,n)来计算,partition时又“跳过”连续相等的一段序列,等等,它的这些“优化”的结果就是速度只有gcc的1/3。
作者:
rockeet
发表日期:
2009年04月15日
分类:
C++
评论:
0 条
阅读次数: 2,075 次
msdn 中有这样一个示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
// 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; } |
但是不能检测某个变量是否有某个成员,象下面这样的代码是不能编译的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
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) { } } |
或许是因为本质上,可以通过其它方法实现这个功能,也就是依据目前的这种机制,也可以达到目的,但是繁琐一些,需要加一个中间层,例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
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()); } } |
DataIO_is_realdump用来推断一个对象是否可以直接通过dump内存来完成序列化,如果可以,在load/save时会有极大的性能提高。 继续阅读 →
代码表示的是数据格式,DATA_IO_LOAD_SAVE 在 <febird/io/DataIO.h> 中定义
对于 boost.serialization
, DATA_IO_LOAD_SAVE
的定义相当于: 继续阅读 →
C++ 程序的性能,重要的不光是运行性能,还有编译性能。运行性能就不多说了,编译性能往往被大家忽略,但是,想一下,如果编译一次要花一小时,那每天能编译几次?
和 boost.serialization 相比,febird.DataIO 的运行性能和编译性能都压倒性地胜出: 继续阅读 →
优化技术主要有两点:
1. 优化的inline
a) 频繁调用的函数都使用inline,但是值得注意的是,在inline的时候,只inline最频繁的分支,很少走到的分支使用非inline函数,例如:
继续阅读 →