主题:[讨论]大家一起来讨论一下c++在这两个编译器上面的问题吧
一下两段程序在vs2008 和dev 上面运行会产生不同的结果
这段代码运行的不同主要是模板参数匹配不同
可能是vs2008没有采用关联参数查找机制
#include<iostream>
#include<algorithm>
#include<typeinfo>
using std::cout;
using std::endl;
void g()
{
cout<<"global g()"<<endl;
}
template<class T>
class Y
{
public:
void g()
{
cout<<"Y<"<<typeid(T).name()<<">::g()"<<endl;
}
void h()
{
cout<<"Y<"<<typeid(T).name()<<">::h()"<<endl;
}
typedef int E;
};
typedef double E;
template<class T> void swap(T& t1,T& t2)
{
cout<<"global swap"<<endl;
T temp;
temp=t1;
t1=t2;
t2=temp;
}
template<class T>
class X:public Y<T>
{
public:
E f()
{
g();
this->h();
T t1=T();
T t2=T(1);
cout<<t1<<endl;
swap(t1,t2);
std::swap(t1,t2);
cout<<typeid(E).name()<<endl;
return E(t2);
}
};
int main()
{
X<int> x;
cout<<x.f();
}
这段代码在dev 和vs2008中得出的结果不同
应该是两种编译器用的处理异常说明机制不同吧
#include<exception>
#include<iostream>
#include<cstdio>
using namespace std;
class A{};
class B{};
void my_thandler()
{
cout<<"terminator caller"<<endl;
exit(0);
}
void my_uhandler1()
{
//cout<<"my_uhandler1 called"<<endl;
throw A();
}
void my_uhandler2()
{
throw ;
}
void t()
{
throw B();
}
void f() throw(A)
{
t();
}
void g() throw(A,bad_exception)
{
t();
}
int main()
{
set_terminate(my_thandler);
set_unexpected(my_uhandler1);
try
{
f();
//cout<<"I will be called"<<endl;
}
catch(A&)
{
cout<<"catch A from f"<<endl;
}
set_unexpected(my_uhandler2);
try
{
g();
}
catch(bad_exception&)
{
cout<<"cauht a bad_expected from g"<<endl;
}
try
{
f();
}
catch(...)
{
cout<<"this will never print"<<endl;
}
}
各位多多讨论一下
小弟谢了
这段代码运行的不同主要是模板参数匹配不同
可能是vs2008没有采用关联参数查找机制
#include<iostream>
#include<algorithm>
#include<typeinfo>
using std::cout;
using std::endl;
void g()
{
cout<<"global g()"<<endl;
}
template<class T>
class Y
{
public:
void g()
{
cout<<"Y<"<<typeid(T).name()<<">::g()"<<endl;
}
void h()
{
cout<<"Y<"<<typeid(T).name()<<">::h()"<<endl;
}
typedef int E;
};
typedef double E;
template<class T> void swap(T& t1,T& t2)
{
cout<<"global swap"<<endl;
T temp;
temp=t1;
t1=t2;
t2=temp;
}
template<class T>
class X:public Y<T>
{
public:
E f()
{
g();
this->h();
T t1=T();
T t2=T(1);
cout<<t1<<endl;
swap(t1,t2);
std::swap(t1,t2);
cout<<typeid(E).name()<<endl;
return E(t2);
}
};
int main()
{
X<int> x;
cout<<x.f();
}
这段代码在dev 和vs2008中得出的结果不同
应该是两种编译器用的处理异常说明机制不同吧
#include<exception>
#include<iostream>
#include<cstdio>
using namespace std;
class A{};
class B{};
void my_thandler()
{
cout<<"terminator caller"<<endl;
exit(0);
}
void my_uhandler1()
{
//cout<<"my_uhandler1 called"<<endl;
throw A();
}
void my_uhandler2()
{
throw ;
}
void t()
{
throw B();
}
void f() throw(A)
{
t();
}
void g() throw(A,bad_exception)
{
t();
}
int main()
{
set_terminate(my_thandler);
set_unexpected(my_uhandler1);
try
{
f();
//cout<<"I will be called"<<endl;
}
catch(A&)
{
cout<<"catch A from f"<<endl;
}
set_unexpected(my_uhandler2);
try
{
g();
}
catch(bad_exception&)
{
cout<<"cauht a bad_expected from g"<<endl;
}
try
{
f();
}
catch(...)
{
cout<<"this will never print"<<endl;
}
}
各位多多讨论一下
小弟谢了