我一次又一次听到这std::move(t)或多或少只是一种奇特的说法static_cast<T&&>(t),不会产生任何指示。当我现在std::move为了更好地理解移动语义而使用 godbolt 时,我发现它确实(或至少可能)生成指令。在这个例子中
#include <iostream>
using namespace std;
struct S {
S() { cout << "default ctor" << endl; }
S(S&& s) {
i = s.i;
s.i = 0;
cout << "move ctor" << endl;
}
int i;
};
void foo(S s) { cout << "Foo called with " << s.i << endl; }
int main() {
S s;
foo(static_cast<S&&>(s));
foo(std::move(s));
}
Run Code Online (Sandbox Code Playgroud)
foo导致以下汇编输出的调用
; ... snip ...
lea rdi, [rbp - …Run Code Online (Sandbox Code Playgroud) 为什么std::move()在这种情况下会导致SEGFAULT?
#include <iostream>
struct Message {
std::string message;
};
Message * Message_Init(std::string message) {
Message * m = (Message*)calloc(1, sizeof(Message));
m->message = std::move(message);
return m;
}
int main() {
auto m = Message_Init("Hello");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
PS请不要问为什么Message不以通常的 C++ 方式构造。
我刚刚遇到std::as_const,并对以下代码片段中最后一行的输出感到惊讶:
#include <cstdio>
#include <utility>
struct S {
void foo() { std::puts("foo: non const"); }
void foo() const { std::puts("foo: const"); }
};
int main() {
S s;
s.foo(); // foo: non const
std::as_const(s).foo(); // foo: const
auto* s_ptr = &s;
s_ptr->foo(); // foo: non const
std::as_const(s_ptr)->foo(); // foo: non const (?)
}
Run Code Online (Sandbox Code Playgroud)
查看文档,我明白为什么调用
非const重载:返回 a ,即对非常量的常量指针的引用,而不是如我所料的a ,即指向常量的指针。foostd::as_const(s_ptr)S* const&SS const*S
所以,我的问题是为什么标准不提供std::as_const指针类型的重载?例如:
template <class T>
constexpr std::add_const_t<T>* as_const(T* t) …Run Code Online (Sandbox Code Playgroud) 当我使用 x86 编译器在 Visual Studio 中执行以下代码(警告级别为 4)时,我看到 C4389:“'==':signed/unsigned Mismatch”编译器警告。
#include <algorithm>
#include <vector>
void functionA()
{
std::vector<int> x(10);
for(size_t i = 0; i < x.size(); i++)
{
if (std::find(x.begin(), x.end(), i) != x.end())
continue;
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释为什么会发生这种情况以及如何解决这个问题吗?
令人兴奋的 的存储库mdspan是为 C++ 标准库建议的多维类似物std::span,现在还包含密切相关的 的参考实现mdarray,与mdspan拥有其数据不同。
但是,尽管该submdspan函数可以生成 的子集mdspan,但我找不到 的类似物mdarray。submdspan我期待的是一个行为与返回完全相同的函数mdspan,但它对 进行操作mdarray。
这是有计划但尚未实施吗?如果没有,为什么不呢?
编辑:
我已经用自制解决方案暂时解决了这个问题,其形式是重载submdspan,mdarray然后创建一个映射mdspan到整个 的临时文件mdarray,并调用submdspan它。
现在它可以完成工作了!但我不确定这是否涵盖了所有可能的情况mdarray,因为目前几乎没有文档。仍然希望得到原来问题的答案。
template <class ElementType, class Extents, class LayoutPolicy, class... SliceSpecs>
auto submdspan(
mdarray<ElementType, Extents, LayoutPolicy> &arr,
SliceSpecs... slices)
{
return submdspan(
mdspan<ElementType, Extents, LayoutPolicy>(arr.data(), arr.mapping()),
slices...);
}
Run Code Online (Sandbox Code Playgroud) 这是来自 hackerrank“继承代码”的示例,
虽然这有效并what()返回n,但如果我注释返回what并取消注释当前注释的部分what()将返回垃圾。
它们在我看来都一样,有什么区别?
/* Define the exception here */
struct BadLengthException : public exception {
public:
int num;
string stra;
BadLengthException(int n){
this->num = n;
this->stra = to_string(this->num);
};
const char * what () const throw () {
return this->stra.c_str();
//string a = to_string(this->num);
//return a.c_str();
}
};
Run Code Online (Sandbox Code Playgroud) 我试图研究作为 C++ 标准库一部分的容器的标准库实现。我的机器上有 Microsoft Visual Studio 2022,我可以转到std::array类的头文件定义。
当我到达类的类定义的末尾时std::array,我注意到数据成员没有声明为私有,并且上面都是公共成员函数,因此也使数据成员成为公共的。
因此,为了测试它,我尝试在std::array定义的对象中访问它main(),令我惊讶的是我可以访问它,并且它也在输出中打印了正确的值!
#include <array>
#include <iostream>
int main()
{
std::array<int, 5> staticArray{0,1,2,3,4};
std::cout << staticArray._Elems[1] << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
这是允许的吗?!任何人都可以破坏数据,对吗?
谢谢大家,我什至不知道用户定义的转换函数及其工作原理。
为什么可以使用std::reference_wrapper<int>::operator+=,如果这样的运算符不存在,是否存在一些隐式转换?
#include <iostream>
#include <functional>
#include <boost/type_index.hpp>
using boost::typeindex::type_id_with_cvr;
template <typename C>
void test(C c)
{
c += 1;
}
int main()
{
int a = 3;
test(a);
std::cout << a << std::endl;
test(std::ref(a));
std::cout << a << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
输出:
3
4
Run Code Online (Sandbox Code Playgroud)
要检查模板是否工作正常:
void test_2(std::reference_wrapper<int> c)
{
c += 1;
}
int main()
{
int a = 3;
test_2(std::ref(a));
std::cout << a << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
输出:
4
Run Code Online (Sandbox Code Playgroud)
仍然像以前一样工作。这怎么可能?
有趣的是,auto d = b + c,中 …
编辑测试用例和编译器不一致。最近我做了一个更改,使用更现代的 < cmath > 而不是 < math.h >。虽然编译得很好,没有任何警告,但对一个文件的这一行更改导致我的测试用例失败。根据我的理解,cmath 只是将大部分函数/变量放在 std 命名空间中,并且我没有对该命名空间进行任何更改。
我会注意到我正在使用
开方
abs (因为我在这里使用双精度,所以我假设编译器会自动使用 cmath/math.h 形式,而不是 stdlib)
晒黑
排除可能受此单行更改影响的任何文件。我确实有一个已经使用 cmath 的不同文件,但不影响测试。
一个测试用例
#include <iostream>
#include <cmath>
int main() {
double x = -0.546;
double y = abs( x / sqrt(x*x + 1.0));
double z = std::abs( x / sqrt(x*x + 1.0));
std::cout << "Variable value is " << x << std::endl;
std::cout << "Value of function using cmath " << z << std::endl;
std::cout << "Value of function using …Run Code Online (Sandbox Code Playgroud) 为什么下面的代码不会死锁。
我尝试用cpp构造死锁情况,但是为什么下面的代码没有死锁
#include <iostream>
#include <memory>
#include <mutex>
#include <thread>
class Request {
public:
void process() {
std::lock_guard<std::mutex> lock(mutex_);
print();
}
void print() {
std::lock_guard<std::mutex> lock(mutex_);
std::cout << "108392" << std::endl;
}
private:
std::mutex mutex_;
};
int main() {
Request req;
req.process();
}
Run Code Online (Sandbox Code Playgroud)
我希望我的代码陷入僵局