我发现了一个影响我工作中的多个单元测试的问题,这种问题仅在使用 valgrind 运行单元测试时才会发生,因为对于相同的输入,从 std::cos 和 std::sin 返回的值是不同的,具体取决于是否单元测试是独立运行的,而不是在 valgrind 下运行。
这个问题似乎只发生在某些特定的输入上,因为许多单元测试都通过了相同的代码。
这是一个最低限度可重现的示例(稍微恶化,以便我的编译器不会优化任何逻辑):
#include <complex>
#include <iomanip>
#include <iostream>
int main()
{
std::complex<long double> input(0,0), output(0,0);
input = std::complex<long double>(39.21460183660255L, -40);
std::cout << "input: " << std::setprecision(20) << input << std::endl;
output = std::cos(input);
std::cout << "output: " << std::setprecision(20) << output << std::endl;
if (std::abs(output) < 5.0)
{
std::cout << "TEST FAIL" << std::endl;
return 1;
}
std::cout << "TEST PASS" << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
正常运行时输出:
input: (39.21460183660254728,-40)
output: (6505830161375283.1118,117512680740825220.91) …Run Code Online (Sandbox Code Playgroud) 我正在尝试调试没有可用源代码的程序,我需要查看它存储在std :: string中的内容.我一直在谷歌搜索并在这里查看,我发现了一些关于输出STL容器的信息,但所有这些都引用了变量,没有源代码或调试信息,我所拥有的是类数据的内存偏移量.有没有办法做到这一点?
我正在做一个小工具来帮助我通过重复记住密码.我想输入密码,每天只记住一次,而不是每次会话之前.当然,我本身不会存储密码,但很乐意存储它的哈希值.
那么,std::string使用C++标准库获取哈希的最简单方法是什么?
因为:
转发列表是一个容器,支持从容器的任何位置快速插入和删除元素
但是没有*std :: forward_list :: push_back*实现.
是否有一种高性能的方法来增加对一个或没有理由的支持?
我想尽可能地用标准C++中的等价物替换外部库(如boost),如果它们存在且可能,最小化依赖性,因此我想知道是否存在转换boost::system::error_code为安全的方法std::error_code.伪代码示例:
void func(const std::error_code & err)
{
if(err) {
//error
} else {
//success
}
}
boost::system::error_code boost_err = foo(); //foo() returns a boost::system::error_code
std::error_code std_err = magic_code_here; //convert boost_err to std::error_code here
func(std_err);
Run Code Online (Sandbox Code Playgroud)
最重要的不是完全相同的错误,只是尽可能接近,最后如果是错误.有智能解决方案吗?
提前致谢!
有两个关于替换不可赋值的向量元素的问题:
对象不可分配的典型原因是其类定义包含const成员,因此将其operator=删除.
std::vector要求其元素类型可分配.事实上,至少使用GCC,当对象不可分配时,直接赋值(vec[i] = x;),erase()以及insert()替换元素的组合都不起作用.
可以使用像下面这样的函数来使用vector::data(),直接元素销毁和使用复制构造函数的新元素来替换元素而不会导致未定义的行为吗?
template <typename T>
inline void replace(std::vector<T> &vec, const size_t pos, const T& src)
{
T *p = vec.data() + pos;
p->~T();
new (p) T(src);
}
Run Code Online (Sandbox Code Playgroud)
下面是一个使用函数的例子.这在GCC 4.7中编译并且似乎有效.
struct A
{
const int _i;
A(const int &i):_i(i) {}
};
int main() {
std::vector<A> vec;
A c1(1);
A c2(2);
vec.push_back(c1);
std::cout << vec[0]._i << std::endl;
/* To replace the element …Run Code Online (Sandbox Code Playgroud) 由于我通常使用的C++编译器允许可变长度数组(例如,数组取决于运行时大小),我想知道是否有类似std::array可变大小的东西?当然std::vector是可变大小,但它在堆上分配,并根据需要重新分配.
我喜欢堆栈分配的数组,其大小在运行时定义.是否有任何std可能具有此功能的模板?也许使用std::vector固定的最大尺寸?
#include <iostream>
#include <memory>
class Base
{
public:
Base() {}
};
class Derived : public Base
{
public:
Derived() {}
Derived(std::initializer_list<std::pair<int, std::shared_ptr<Base>>>) {}
};
int main(int argc, char ** argv)
{
auto example = new Derived({
{ 0, std::make_shared<Derived>() }
});
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它的工作原理(实时预览)正常,但是当我尝试使用std::make_shared与std::initializer_list作为参数,我得到了错误:
auto example = new Derived({
{ 0, std::make_shared<Derived>({
{ 0, std::make_shared<Derived>() }
}) }
});
Run Code Online (Sandbox Code Playgroud)
正如您在实时预览中看到的那样.
错误:功能参数太多......
它仅适用于我这样做(实时预览):
auto example = new Derived({
{ 0, …Run Code Online (Sandbox Code Playgroud) 我想说这个:
<script src = "Script2.js" type = "text/javascript"> < / script>
Run Code Online (Sandbox Code Playgroud)
在一个std::string所以我\在每个双引号(")之前附加一个()符号,给它一个字面含义",而不是像这样的C++中的字符串分界:
std::string jsFilesImport = "<script src = \"Script2.js\" type = \"text/javascript\"> < / script>\""
Run Code Online (Sandbox Code Playgroud)
如果我有string很多("),则\每个(")的添加()变得困难.有一种简单的方法可以在C++中实现这一点吗?
说我有一个std::optional<T>.我重置并多次赋值.对于给定的可选值,值的地址(如果存在)是否始终相同?
换一种说法:
#include <cassert>
#include <optional>
#include <string>
template<typename T>
auto test()
{
auto opt = std::optional<T>{T{}};
auto* ptr = &*opt;
opt.reset();
opt = T{};
assert(ptr == &*opt); // Can this assert fail?
}
int main()
{
test<int>();
test<double>();
test<std::string>();
// ...
}
Run Code Online (Sandbox Code Playgroud)
标准是否保证了值的地址的稳定性?
c++ ×10
std ×10
c++11 ×4
string ×2
boost ×1
c++17 ×1
cryptography ×1
error-code ×1
forward-list ×1
gdb ×1
hash ×1
precision ×1
stl ×1
valgrind ×1
vector ×1