我想对一些返回对象引用的成员函数应用__declspec(nothrot)
。例如,我想将其添加到此函数(在 MyClass.h 中):
CMyClass& operator= ( IN UInt32 nValue )
{
return ( SetValue ( nValue ) );
};
VC++ documentation says我需要将它添加到函数的返回类型之后,如下所示:
返回类型 __declspec(nothrow) [调用约定] 函数名称 ([参数列表])
如果我遵循这一点,我的大多数函数都可以与__declspec(nothrot)
一起正常工作。但是,当我对上述成员函数(或返回 CMyClass&
的任何其他函数)执行此操作时,我收到编译错误:
错误 C2059:语法错误:'__declspec(nothrow)'
我可以将它放在返回类型前面,然后编译,但文档还说,如果 __declspec() 放置在错误的位置,编译器可能会忽略它,而不会发出警告或错误。
使用__declspec(nothrow)
的正确方法是什么?
最佳答案
我相信(但不是 100% 确定)正确的语法是:
CMyClass __declspec(nothrow) & operator= ( IN UInt32 nValue )
编译器似乎对函数声明遵循与简单声明相同的规则,并且 &
(或 *
)在 declspec
之后。这与 MSDN documentation 中的一些匹配.
The __declspec keywords should be placed at the beginning of a simple declaration. The compiler ignores, without warning, any __declspec keywords placed after * or & and in front of the variable identifier in a declaration.
不过,正如其他人指出的那样,这是浪费时间。 throw()
没有同样奇怪的解析问题,并且 the documentation特别指出它是等效的。
Given the following preprocessor directive, the three function declarations below are equivalent:
block 引用>#define WINAPI __declspec(nothrow) __stdcall
void WINAPI f1();
void __declspec(nothrow) __stdcall f2();
void __stdcall f3() throw();
关于c++ - 在返回引用的函数上使用 __declspec(nothrow),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9168424/