考虑下面的代码及其输出
#include <cstdint>
#include <iostream>
using std::cout;
using std::endl;
class alignas(8) Something {
std::uint8_t integer{};
};
int main() {
auto something = Something{};
auto character = std::uint8_t{};
cout << sizeof(something) << endl;
cout << reinterpret_cast<std::uintptr_t>(&something) << endl;
cout << reinterpret_cast<std::uintptr_t>(&character) << endl;
}
Run Code Online (Sandbox Code Playgroud)
https://wandbox.org/permlink/m6D0PYWyrGlfjYJP。一次运行的输出
8
140729604143976
140729604143975
Run Code Online (Sandbox Code Playgroud)
如果我有一个对齐到8个字节的结构,则对其进行调用sizeof会将结构的大小四舍五入到其对齐的最接近倍数。但是编译器仍然能够将对象放置在向上取整后剩下的假设空间中。
在什么情况下允许?总是允许吗?sizeof如果不尊重尺寸,为什么要在此处四舍五入?
参考下面的代码
auto x = std::atomic<std::uint64_t>{0};
auto y = std::atomic<std::uint64_t>{0};
// thread 1
x.store(1, std::memory_order_release);
auto one = y.load(std::memory_order_seq_cst);
// thread 2
y.fetch_add(1, std::memory_order_seq_cst);
auto two = x.load(std::memory_order_seq_cst);
Run Code Online (Sandbox Code Playgroud)
这里有可能one和two都为 0 吗?
(我似乎遇到了一个错误,在上面的代码运行后,如果one和two都可以保持 0 的值,则可以解释该错误。并且排序规则太复杂,我无法弄清楚上面可以进行哪些排序。)
在clang上运行以下代码会因分段错误而退出
#include <stdexcept>
int foo() {
throw std::runtime_error{{}};
}
int main() {
try {
throw foo();
} catch (...) {}
}
Run Code Online (Sandbox Code Playgroud)
https://wandbox.org/permlink/PrLRJyHq9o2K5Eez
但这在gcc https://wandbox.org/permlink/ORV2B5RfTl22RKxo上运行良好。阅读标准似乎没有明确表明这种事情是无效的。c在这里错了吗?
为什么连接的结果是以下路径/c/d?
std::filesystem::path{"/a"} / std::filesystem::path{"b"} / std::filesystem::path{"/c/d"}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我的心智模型将结果路径设置为/a/b/c/d。令我惊讶的是,这很简单/c/d。很想知道我在这里哪里出错了。(以及正确的心智模型是什么)
所以我对JavaScript很新,我知道它没有真正的并发线程.但是如果你想象一个ajax回调在它自己的线程中并且该回调在屏幕上绘制一些按钮然后调用一个函数wire_up_buttons()来为所有可用按钮设置点击事件的回调.从主线程调用该函数是否是好的风格(即从$(document).ready(callback)哪个开始执行?
如果这被认为是糟糕的风格那么你会如何建议我改变我的代码?
参考以下代码
#include <iostream>
#include <memory>
using std::cout;
using std::endl;
using std::make_unique;
struct Base {};
struct Derived : public Base {};
int main() {
auto base_uptr = std::unique_ptr<Base>{make_unique<Derived>()};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
哪个构造函数被调用unique_ptr?我看了一下cppreference.com和我发现的构造函数(前c ++ 17)(为了完成)
constexpr unique_ptr();
constexpr unique_ptr( nullptr_t );
explicit unique_ptr( pointer p );
unique_ptr( pointer p, /* see below */ d1 );
unique_ptr( pointer p, /* see below */ d2 );
unique_ptr( unique_ptr&& u );
template< class U, class E >
unique_ptr( unique_ptr<U, E>&& u …Run Code Online (Sandbox Code Playgroud) 我不确定是否double有一个标准化的表示,这就是我想问的原因,是否有一种很好的方法来编写一个assert确保a中的值double足够小以适应一个int64_t?
更具体地说int64_t,a和a 之间的比较double可能超出了前者的范围,并且保证是正确的?
我的印象是以下容易发生泄漏
class Something {
std::unique_ptr<A> a;
std::unique_ptr<int> b{new int{3}};
std::unique_ptr<C> c;
public:
Something() : a{new A{}}, c{new C{}} {};
};
Run Code Online (Sandbox Code Playgroud)
如果评估顺序如下
new A{}new int{3}new C{}a{}b{}c{}我正在查看新的C++ 17功能(http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0145r3.pdf),该功能修复了之前评估错误的顺序C++的版本,但它似乎只谈论函数参数评估.
我查看了关于评估顺序的cppreference文档(http://en.cppreference.com/w/cpp/language/eval_order),它似乎也没有提到这一点.
以上是否有明确的评估顺序?(现在使用C++ 17或以前版本的C++)
我有一个类层次结构,其中有一个Base带有实现列表的基类型和另一个基类,AnotherBase几乎相同,Base但略有不同。为了用语言表达这一点,我在第二个基类上使用了私有继承(因此后者与前者的实现之间没有原样的关系)。
假设这是代码(https://wandbox.org/permlink/2e2EG0eKmcLiyvgt)
#include <iostream>
using std::cout;
using std::endl;
class Base {
public:
virtual ~Base() = default;
virtual void foo() = 0;
};
class Impl : public Base {
public:
void foo() {
cout << "Impl::foo()" << endl;
}
};
class AnotherBase : private Base {
public:
using Base::foo;
// other virtual methods
};
class Derived : public AnotherBase {
public:
explicit Derived(std::unique_ptr<Base> base) : base_{std::move(base)} {}
void foo() override {
base_->foo();
} …Run Code Online (Sandbox Code Playgroud) c++ ×8
c++17 ×5
c++11 ×2
ajax ×1
c++20 ×1
double ×1
exception ×1
filesystems ×1
http ×1
inheritance ×1
int ×1
javascript ×1
jquery ×1
overflow ×1
path ×1
polymorphism ×1
stdatomic ×1
struct ×1
unique-ptr ×1