昨晚我有一个有趣的想法,捕获硬件异常并抛出一个C++ exception
。认为这可能对诸如 之类的事情有用FPU exceptions
,这些事情通常要么崩溃,要么默默返回NaN
,然后导致意外行为。AC++ exception
在这里更可取。
所以我整个上午都在进行黑客攻击,终于让它工作了。嗯,差不多。编译器仍然没有意识到算术运算现在可以 throw C++ exceptions
,并且会默默地丢弃try/catch
它周围的块。当异常发生在函数中时它确实起作用。
void throw_exception()
{
throw std::runtime_error("Division by zero!");
}
__attribute__((noinline))
void try_div0()
{
cout << 1 / 0 << endl;
}
int main()
{
// this class traps a hardware exception (division by zero, in this case) and calls the supplied lambda function.
// uh, no, you probably don't want to see the assembly code behind this...
exception_wrapper div0_exc { 0, …
Run Code Online (Sandbox Code Playgroud) 我试图让 GCC 内联一个特定函数中的所有调用,并不管全局优化设置如何对其进行优化。这flatten
属性似乎符合我的要求,但仅当使用相同的 -O 设置编译整个源文件时。当指定 both 时__attribute__((flatten, optimize("O2")))
,如果源文件的其余部分是使用 -O2 以外的任何其他设置编译的,则 GCC 显然会忽略 flatten 属性。
这个特定的函数是一个中断例程,我需要确保整个函数包含在一个连续的内存块中。唯一的函数调用是 to __builtin_ia32_rdtsc()
(它只包含rdtsc
和ret
)和两个std::atomic
操作。
那么,正如标题所说,有没有办法强制这些调用被内联和优化?
我正在尝试使用一个库(Watt-32,如果它是相关的),由于某种原因无法链接.我编译了库并作为一个快速的"hello world"测试,我正在尝试编译以下文件:
#include <tcp.h>
int main() { sock_init(); }
Run Code Online (Sandbox Code Playgroud)
这将导致GCC来产生一个长的列表multiple definition
错误,在图书馆的自己的源文件:
D:\projects\test-tcp>c++ -Iinclude test-tcp.cpp -Llib -lwatt
lib\libwatt.a(rs232.o): In function `_ntohl':
D:\msys64\home\JW\watt32\src/../inc/sys/swap.h:63: multiple definition of `__ntohl'
lib\libwatt.a(pctcp.o):D:\msys64\home\JW\watt32\src/../inc/sys/swap.h:63: first defined here
lib\libwatt.a(rs232.o): In function `_ntohs':
D:\msys64\home\JW\watt32\src/../inc/sys/swap.h:73: multiple definition of `__ntohs'
lib\libwatt.a(pctcp.o):D:\msys64\home\JW\watt32\src/../inc/sys/swap.h:73: first defined here
lib\libwatt.a(rs232.o): In function `get_fs_reg':
D:\msys64\home\JW\watt32\src/misc.h:736: multiple definition of `get_fs_reg'
lib\libwatt.a(pctcp.o):D:\msys64\home\JW\watt32\src/misc.h:736: first defined here
lib\libwatt.a(rs232.o): In function `get_gs_reg':
D:\msys64\home\JW\watt32\src/misc.h:744: multiple definition of `get_gs_reg'
lib\libwatt.a(pctcp.o):D:\msys64\home\JW\watt32\src/misc.h:744: first defined here
lib\libwatt.a(rs232.o): In function `set_fs_reg':
D:\msys64\home\JW\watt32\src/misc.h:751: multiple definition of …
Run Code Online (Sandbox Code Playgroud) 在下面的类中,我定义了operator()
返回向量return_T
:
#include <vector>
template <typename return_T, typename ... arg_T>
class A
{
public:
std::vector<return_T> operator()(arg_T... args);
};
Run Code Online (Sandbox Code Playgroud)
这是有效的,除了 的情况return_T = void
,因为 avector<void>
是不可能的。所以我需要以A<void, arg_T>::operator()
某种方式定义一个专业化。我正在尝试以下代码:
#include <vector>
template <typename return_T, typename ... arg_T>
class A
{
public:
auto operator()(arg_T... args);
};
template<typename return_T, typename... arg_T>
auto A<return_T, arg_T...>::operator()(arg_T... args) -> typename std::enable_if<!std::is_void<return_T>::value, std::vector<return_T>>::type
{ }
template<typename return_T, typename... arg_T>
auto A<void, arg_T...>::operator()(arg_T... args) -> void
{ }
Run Code Online (Sandbox Code Playgroud)
但编译器不喜欢它。
error : prototype for …
Run Code Online (Sandbox Code Playgroud)