我有以下代码,我希望decltype()在Derived类上不能使用run()基类方法返回类型,因为基类没有默认构造函数.
class Base
{
public:
int run() { return 1; }
protected:
Base(int){}
};
struct Derived : Base
{
template <typename ...Args>
Derived(Args... args) : Base{args...}
{}
};
int main()
{
decltype(Derived{}.run()) v {10}; // it works. Not expected since
// Derived should not have default constructor
std::cout << "value: " << v << std::endl;
//decltype(Base{}.run()) v1 {10}; // does not work. Expected since
// Base does not have default constructor
//std::cout << "value: …Run Code Online (Sandbox Code Playgroud) 我正在std::terminate通过使用用于抛出、抛出的类的复制构造函数来测试在进行堆栈展开调用时如何抛出。
根据 C++14 N4296 - §15.1.3:
抛出异常复制初始化 (8.5, 12.8) 一个临时对象,称为异常对象。临时是一个左值,用于初始化在匹配处理程序 (15.3) 中声明的变量。
class X
{
public:
X() = default;
X(const X&)
{
throw std::runtime_error{"X copy constructor"};
}
};
int main()
{
try
{
throw X{};
}
catch(const std::exception& e)
{
std::cout << e.what() << std::endl; // this line gets executed, does not terminate
}
catch(const X& other)
{
std::cout << "X exception" << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
为什么上面的代码没有调用std::terminate?根据我对上述代码的理解, ,throw X{}内部try开始堆栈缠绕,然后调用复制构造函数来调用处理程序,但复制构造函数也会抛出。请注意,如果更改复制构造函数的 throw 表达式,throw …
我正在检查编译器如何为x86_64上的多核内存屏障发出指令。以下代码是我正在测试的代码gcc_x86_64_8.3。
std::atomic<bool> flag {false};
int any_value {0};
void set()
{
any_value = 10;
flag.store(true, std::memory_order_release);
}
void get()
{
while (!flag.load(std::memory_order_acquire));
assert(any_value == 10);
}
int main()
{
std::thread a {set};
get();
a.join();
}
Run Code Online (Sandbox Code Playgroud)
使用时std::memory_order_seq_cst,我可以看到该MFENCE指令用于任何优化-O1, -O2, -O3。该指令确保刷新了存储缓冲区,因此在L1D缓存中更新了它们的数据(并使用MESI协议确保其他线程可以看到效果)。
但是,当我std::memory_order_release/acquire不进行优化MFENCE使用时,也会使用指令,但是使用-O1, -O2, -O3优化会忽略该指令,并且不会看到其他刷新缓冲区的指令。
在MFENCE不使用的情况下,如何确保将存储缓冲区数据提交给高速缓存以确保内存顺序语义?
以下是使用get / set函数的汇编代码-O3,例如我们在Godbolt编译器资源管理器中获得的代码:
set():
mov DWORD PTR any_value[rip], 10
mov BYTE PTR flag[rip], 1
ret
.LC0:
.string …Run Code Online (Sandbox Code Playgroud)