回 帖 发 新 帖 刷新版面

主题:[讨论]大家一起来讨论一下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;
           }
    
}



各位多多讨论一下

小弟谢了

回复列表 (共5个回复)

沙发

楼主先来:)

板凳


我觉得这个是编译器的问题   dev用的是gun编译器     是符合标准c++的


希望各位能给出高见     vs2008中应该是可以设置一些参数来让第一段程序采用关联参数机制的

3 楼

问题1:
我记得周星星前辈在几千年前曾讲过VC的运行时类型识别相当烂(在VCKBase里),但这东东确实非常少用,也尽量不要用~~~~不知道现在有改进没有。但编译选项里好像默认是关闭的~~~~~(RTTI)

4 楼

问题2:
VC中如果你用调试方式执行,或是使用调试版程序。那么程序将会特殊“关照”一下异常。你会得不到你所期望的东东:)

5 楼


谢谢大家了   我找到好的解决方法了就回复    希望大家继续给出意见

我来回复

您尚未登录,请登录后再回复。点此登录或注册