问题描述
我使用Visual Studio .NET(2003),Visual Studio 2008和Visual Studio 2010编译了以下代码:
I compiled the following code using Visual Studio .NET (2003), Visual Studio 2008, and Visual Studio 2010:
#include< iostream>
#include< vector>
#include< algorithm>
#include< functional>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
使用命名空间标准;
B级
{
int x;
公开:
B():x(0){}
virtual int getx(){return x; }
虚拟〜B(){}
};
class B
{
int x;
public:
B(): x(0) {}
virtual int getx() { return x; }
virtual ~B() {}
};
D1类:公共B
{
int y;
公开:
D1():y(0){}
void sety(int yy){y = yy; }
int gety(){返回y; }
};
class D1: public B
{
int y;
public:
D1(): y(0) {}
void sety(int yy) { y = yy; }
int gety() { return y; }
};
D2类:公共B
{
int z;
公开:
D2():z(0){}
无效setz(int zz){z = zz; }
int getz(){return z; }
};
class D2: public B
{
int z;
public:
D2(): z(0) {}
void setz(int zz) { z = zz; }
int getz() { return z; }
};
向量< B *> vb;
vector<B*> vb;
template< H1类,H2类>
无效ProcessB(B * b,H1 h1,H2 h2)
{
if(D1 * d1 = dynamic_cast< D1 *>(b))h1(d1);
否则if(D2 * d2 = dynamic_cast< D2 *>(b))h2(d2);
}
template<class H1, class H2>
void ProcessB(B* b, H1 h1, H2 h2)
{
if(D1* d1 = dynamic_cast<D1*>(b)) h1(d1);
else if(D2* d2 = dynamic_cast<D2*>(b)) h2(d2);
}
void ProcessD1(D1 * d1)
{
d1-> sety(2);
cout<< 处理D1"指的是处理D1". << endl;
}
void ProcessD1(D1* d1)
{
d1->sety(2);
cout << "Processing D1" << endl;
}
void ProcessD2(D2 * d2)
{
d2-> setz(5);
cout<< 处理D2"指的是处理D2". << endl;
}
void ProcessD2(D2* d2)
{
d2->setz(5);
cout << "Processing D2" << endl;
}
类ProcessBFO
{
无效ProcessD1FO(D1 * d1)
{
d1-> sety(2);
cout<< 处理D1"指的是处理D1". << endl;
}
class ProcessBFO
{
void ProcessD1FO(D1* d1)
{
d1->sety(2);
cout << "Processing D1" << endl;
}
无效ProcessD2FO(D2 * d2)
{
d2-> setz(5);
cout<< 处理D2"指的是处理D2". << endl;
}
公开:
void operator()(B * b){ProcessB(b,bind1st(mem_fun(& ProcessBFO :: ProcessD1FO),this),bind1st(mem_fun(& ProcessBFO :: ProcessD2FO),this)); }
};
void ProcessD2FO(D2* d2)
{
d2->setz(5);
cout << "Processing D2" << endl;
}
public:
void operator()(B* b) { ProcessB(b, bind1st(mem_fun(&ProcessBFO::ProcessD1FO), this), bind1st(mem_fun(&ProcessBFO::ProcessD2FO), this)); }
};
int main()
{
vb.push_back(new D2());
vb.push_back(new D1());
vb.push_back(new D2());
int main()
{
vb.push_back(new D2());
vb.push_back(new D1());
vb.push_back(new D2());
for_each(vb.begin(),vb.end(),ProcessBFO());
返回0;
}
这只是一些测试代码,所以请不要对错误的变量或函数名称等发火.
for_each(vb.begin(), vb.end(), ProcessBFO());
return 0;
}
This is just some test code, so please don't flame me about bad variable or function names, etc.
要注意的一件事是,如果我使用以下内容重新定义ProcessBFO :: operator(),则Visual Studio 2003会完美地内联它:
One thing to note is that if I repoace the definition of ProcessBFO::operator() with the following, Visual Studio 2003 inlines it perfectly:
void operator()(B * b){ProcessB(b,ProcessD1,ProcessD2); }
void operator()(B* b) { ProcessB(b, ProcessD1, ProcessD2); }
因此,换句话说,它可以轻松地内嵌独立功能,但不能很容易地内联指向成员的指针...
So, in other words, it can inline free standing functions easily, but it cannot inline pointers to members very easily...
欢迎提出任何建议.
推荐答案
如果在较新的编译器中效果更好(考虑到VS 2008和VS 2010设计用于不支持VS 2003的现代Windows),则结论很明显.
If the result is better in the newer compilers, (considering that VS 2008 and VS 2010 are designed to be used in modern Windows, where VS 2003 is not supported), the conclusion is obvious.
这篇关于Visual C ++ 2003 Optimizer不好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!