相关疑难解决方法(0)

可以标记为Extern"C"的C++函数抛出吗?

我有C++函数我想要声明使用,extern "C"即使它们只在C++代码中调用.是的,我知道这很奇怪但是我想要保持一致性,因为我们混合了C和C++声明.我只是想确保声明一个C++函数extern "C"不会影响抛出的行为.

它看起来像这样:

extern "C" void foo() {throw exception;}

int bar()
{
    try
    {
        foo();
    } catch (exception e) { return 1; }
}
Run Code Online (Sandbox Code Playgroud)

c++ extern-c

25
推荐指数
2
解决办法
4683
查看次数

gcc - 属性nothrow用于什么?

我正在浏览一些gcc属性列表,我发现这个引起我注意的那个:

nothrow
The nothrow attribute is used to inform the compiler that a function cannot
throw an exception. For example, most functions in the standard C library can be
guaranteed not to throw an exception with the notable exceptions of qsort and
bsearch that take function pointer arguments. The nothrow attribute is not
implemented in GCC versions earlier than 3.3. 
Run Code Online (Sandbox Code Playgroud)

C函数如何抛出异常?有人可以解释这个属性用于什么?

似乎有一个nothrow标签可用,但我发现那里似乎与C++有关std::nothrow.不确定这是否与我的特定问题有关.

c gcc exception

5
推荐指数
1
解决办法
1060
查看次数

串行/套接字IO和GCC nothrow属性

我具有抽象用C实现的串行套接字IO(Linux / Windows)的功能。所有这些都被标记为,extern "C"因为它们也可以从C ++中调用。

在这里使用__attribute__((__nothrow__))(或MinGW Macro __MINGW_NOTHROW)是否安全?我可以假设没有抛出异常吗?

调用的函数-套接字:(未列出WinSock的所有新增功能

  • socket
  • connect
  • send / recv
  • closeclosesocket在Windows上)
  • sendto / recvfrom

所谓的函数-串行:
由于Windows / Linux之间的串行IO代码差异很大,因此此处未列出所有代码

  • Linux(GNU)
    • open
    • tcgetattr
    • read / write
    • close
  • Windows(MinGW)
    • CreateFile
    • GetCommState / SetCommTimeouts
    • ReadFile / WriteFile
    • CloseHandle

由于ANSI C没有例外(如果我错了,请纠正我),它们不会被抛出,但是GCC扩展和OS API调用又如何呢?

文档:http : //gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html(请参阅参考资料nothrow)。

c io gcc mingw

1
推荐指数
1
解决办法
980
查看次数

标签 统计

c ×2

gcc ×2

c++ ×1

exception ×1

extern-c ×1

io ×1

mingw ×1