问个异常规范的问题
我在vs 2008中写了个程序,其中用到了异常规范,但是vs 2008却忽略了异常规范。请问vs 2008支持标准C++的异常规范不?IntArray.h文件如下:
#ifndef INTARRAY_H
#define INTARRAY_H
#include <iostream>
using namespace std;
class OutOfBoundsException
{
public:
OutOfBoundsException(int);
int offset() const;
private:
int _offset;
};
inline OutOfBoundsException::OutOfBoundsException(int offset):
_offset(offset)
{}
inline int OutOfBoundsException::offset() const
{
return _offset;
}
class IntArray
{
public:
IntArray(int);
IntArray(int *, int);
IntArray(const IntArray &);
~IntArray();
int size() const;
IntArray &operator =(const IntArray &);
bool operator ==(const IntArray &);
int &operator [](int) const;
friend ostream &operator <<(ostream &, const IntArray &);
private:
int _size;
int *pArray;
};
inline IntArray::IntArray(int size):
_size(size)
{
pArray = new int[_size];
for(int ix=0;ix<_size;++ix)
pArray[ix] = 0;
}
inline IntArray::IntArray(int *p, int size):
_size(size)
{
pArray = new int[_size];
for(int ix=0;ix<_size;++ix)
pArray[ix] = p[ix];
}
inline IntArray::IntArray(const IntArray &rhs):
_size(rhs._size)
{
pArray = new int[_size];
for(int ix=0;ix<_size;++ix)
pArray[ix] = rhs.pArray[ix];
}
inline IntArray::~IntArray()
{
delete [] pArray;
pArray = 0;
}
inline int IntArray::size() const
{
return _size;
}
inline IntArray &IntArray::operator =(const IntArray &rhs)
{
if(this != &rhs)
{
delete [] pArray;
int size = rhs._size;
pArray = new int[size];
for(int ix=0;ix<size;++ix)
pArray[ix] = rhs.pArray[ix];
}
return *this;
}
inline bool IntArray::operator ==(const IntArray &rhs)
{
if(_size != rhs._size)
return false;
else
{
int ix = 0;
for(;ix<_size;++ix)
{
if(pArray[ix] != rhs.pArray[ix])
break;
}
if(ix < _size)
return false;
else
return true;
}
}
inline int &IntArray::operator [](int offset) const throw(OutOfBoundsException)
{
if(offset >= _size)
throw OutOfBoundsException(offset);
else
return pArray[offset];
}
#endif
IntArray.cpp文件如下:
#include "IntArray.h"
#include <iostream>
#include <fstream>
using namespace std;
ostream &operator <<(ostream &os, const IntArray &intArray)
{
for(int ix=0;ix<intArray._size;++ix)
cout << intArray[ix] << " ";
return os;
}
int main()
{
int a[] = {35,16,31,47,33,74,21,57,21,80};
IntArray intArray(a,sizeof(a)/sizeof(a[0]));
cout << intArray << endl;
try
{
cout << intArray[10] << endl;
}
catch(OutOfBoundsException &oobe)
{
cerr << "下标[" << oobe.offset() << "]已经超出了数组的界限!" << endl;
}
return 0;
}
但是编译有警告,编译信息如下:
1>------ 已启动生成: 项目: CP11, 配置: Debug Win32 ------
1>正在编译...
1>IntArray.cpp
1>f:\program files\source\cp11\cp11\intarray.h(119) : warning C4290: 忽略 C++ 异常规范,但指示函数不是 __declspec(nothrow)
1>生成日志保存在“file://f:\Program Files\source\CP11\CP11\Debug\BuildLog.htm”
1>CP11 - 0 个错误,1 个警告
========== 生成: 成功 1 个,失败 0 个,最新 0 个,跳过 0 个 ==========