我正在尝试调用本机机器语言代码.这是我到目前为止(它得到一个总线错误):
char prog[] = {'\xc3'}; // x86 ret instruction
int main()
{
typedef double (*dfunc)();
dfunc d = (dfunc)(&prog[0]);
(*d)();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它确实正确地调用了函数并且它到达了ret指令.但是当它试图执行ret指令时,它有一个SIGBUS错误.是因为我在一个未清除执行的页面上执行代码或类似的东西?
那么我在这里做错了什么?
#include <tuple>
int main()
{
int xa = 1;
int ya = 2;
auto const& [xb, yb] = std::tuple<int&, int&>(xa, ya);
xb = 9; // Shouldn't this be read-only?
return xa + ya;
}
Run Code Online (Sandbox Code Playgroud)
这不仅编译,而且返回11.
所以有两个问题:
为什么在指定为自动const&?时允许写入xb?这不应该编译吗?
为什么我不能用"auto&"替换"auto const&"并将其编译?Clang(6.0)和g ++(7.3)都抱怨错误消息,例如"类型<...>的非常量左值引用无法绑定到类型元组的临时<...>"
谢谢!
我在下面有一个简单的代码片段,它使用以下编译:
g++-9 -std=c++2a -fconcepts
这是试图定义一个需要函数存在的概念。我希望输出是“是”,但它不是......知道为什么吗?谢谢。
#include <iostream>
template <typename T>
concept bool HasFunc1 =
requires(T) {
{ T::func1() } -> int;
};
struct Test
{
int func1()
{
return 5;
}
};
int main()
{
if constexpr (HasFunc1<Test>)
std::cout << "yes\n";
}
Run Code Online (Sandbox Code Playgroud) 我知道curl可以将数据发布到URL:
$ curl -X POST http://httpbin.org/post -d "hello"
{
"args": {},
"data": "",
"files": {},
"form": {
"hello": ""
},
"headers": {
"Accept": "*/*",
"Content-Length": "5",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "curl/7.50.1"
},
"json": null,
"origin": "64.238.132.14",
"url": "http://httpbin.org/post"
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过管道传递curl来实现同样的事情:
$ echo "hello" | curl -X POST http://httpbin.org/post -d @-
{
"args": {},
"data": "",
"files": {},
"form": {
"hello": ""
},
"headers": {
"Accept": "*/*",
"Content-Length": "5",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "curl/7.50.1"
},
"json": null,
"origin": "64.238.132.14", …Run Code Online (Sandbox Code Playgroud) 我有一个非常简单的数据集(30 行,32 列)。
我编写了一个 Python 程序来加载数据并训练 XGBoost 模型,然后将模型保存到磁盘。
我还编译了一个使用 libxgboost (C api) 的 C++ 程序并加载模型进行推理。
当使用相同的保存模型时,Python 和 C++ 对于相同的输入(单行全零)给出不同的结果。
xgboost 是 0.90,我已在此处附加了所有文件(包括 numpy 数据文件):
https://www.dropbox.com/s/txao5ugq6mgssz8/xgboost_mismatch.tar?dl=0
以下是两个程序的输出(其源代码位于 .tar 文件中):
(在构建模型时打印一些字符串,然后打印单个数字输出)
$ python3 jl_functions_tiny.py
Loading data
Creating model
Training model
Saving model
Deleting model
Loading model
Testing model
[587558.2]
Run Code Online (Sandbox Code Playgroud)
(它发出的单个数字显然与单个 Python 数字输出不匹配)
$ ./jl_functions
628180.062500
Run Code Online (Sandbox Code Playgroud) 我的术语很糟糕,所以这个术语值得一些解释。想象一下我有一个像这样的 DataFrame(我称之为“长”表):
time stock price
---------------------------
13:03:00 AAPL 100.00
13:03:00 SPY 200.00
13:03:01 AAPL 100.01
13:03:02 SPY 200.01
13:03:03 SPY 200.02
.
.
.
Run Code Online (Sandbox Code Playgroud)
我想将其转换为这样的 DataFrame(我称之为“宽而稀疏”表):
time AAPL SPY
---------------------------
13:03:00 100.00 200.00
13:03:01 100.01 Nan
13:03:02 Nan 200.01
13:03:03 Nan 200.02
Run Code Online (Sandbox Code Playgroud)
显然这是一个很大的转变。是否有内置函数可以执行此操作?看起来这可能是一件很常见的事情。
谢谢!
我正在尝试捕获(按值)整个 C 样式数组。该数组似乎衰减为一个指针......我如何防止这种情况以便捕获整个数组?
代码:
#include <iostream>
int main()
{
char x[1024];
std::cerr << sizeof(x) << "\n";
[x = x] // copy
{
std::cerr << sizeof(x) << "\n";
}();
}
Run Code Online (Sandbox Code Playgroud)
这打印:
1024 <<-- yay
8 <<-- oops... not a true copy
Run Code Online (Sandbox Code Playgroud)
应该注意的是,这如我所愿(两个结果均为 1024):
#include <iostream>
#include <array>
int main()
{
std::array<char, 1024> x;
std::cerr << sizeof(x) << "\n";
[x = x] // copy
{
std::cerr << sizeof(x) << "\n";
}();
}
Run Code Online (Sandbox Code Playgroud) 我使用 CMake 和 clang 进行编译。我仅使用 clangd VSCode 扩展(即不使用任何其他 C++ 扩展)。
在“问题”窗口中,该窗口由 VSCode 提供支持,而不是编译器输出(因此我的 CMake 内容或compile_commands.json 没有问题,因为我什至不必编译即可查看问题消息):
第一个错误可能来自 clangd,我想保留它。我怎样才能摆脱第二个错误?我没有在构建中的任何地方使用 GCC,所以我不知道它是如何到达那里的。
谢谢!
我希望一个类具有一个带有模板参数的函数,并且基于该模板参数操作一个特定的成员变量.
例如,如果允许函数模板特化,那么像这样:
struct A
{
struct M1 {};
struct M2 {};
// Function template specialization not allowed :(
template<typename M>
void addM(M const &m);
template<>
void addM(M1 const &m)
{
m1_vec_.push_back(m);
}
template<>
void addM(M2 const &m)
{
m2_vec_.push_back(m);
}
std::vector<M1> m1_vec_;
std::vector<M2> m2_vec_;
};
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?我觉得我错过了一些简单的东西,但不能完全把手指放在上面.
我有一个对象'S'存储一个简单的指针元组,通过使用可变参数模板使其变得灵活.有两种方法,store()和store2().第一个(商店)工作正常.第二个将无法编译,因为std :: make_tuple失败并显示错误:
'调用'make_tuple'没有匹配函数
它进一步补充说,第一个参数没有从'B*'到'B*&&'的已知对话(这个错误在元组库头中很深).
代码在这里:
#include <tuple>
#include <utility>
template<typename...Rs>
struct S
{
void store(std::tuple<Rs*...> rs)
{
rs_ = rs;
}
void store2(Rs*...rs)
{
rs_ = std::make_tuple<Rs*...>(rs...); // This is the guy that breaks
}
private:
std::tuple<Rs*...> rs_;
};
struct B
{};
struct A : S<B, B>
{};
int main()
{
auto *b1 = new B;
auto *b2 = new B;
auto *a1 = new A;
a1->store(std::make_tuple(b1, b2)); // This works
a1->store2(b1, b2); // How can I get this …Run Code Online (Sandbox Code Playgroud)