在 C++ 中,是否可以从我以后不再需要的地图中窃取资源?更准确地说,假设我有一个std::map
withstd::string
键,我想通过map
使用std::move
. 请注意,对键的这种写访问会破坏 的内部数据结构(键的顺序),map
但之后我不会使用它。
问题:我可以在没有任何问题的情况下执行此操作,还是会导致意外错误,例如在析构函数中出现意外错误,map
因为我std::map
以非预期的方式访问它?
这是一个示例程序:
#include<map>
#include<string>
#include<vector>
#include<iostream>
using namespace std;
int main(int argc, char *argv[])
{
std::vector<std::pair<std::string,double>> v;
{ // new scope to make clear that m is not needed
// after the resources were stolen
std::map<std::string,double> m;
m["aLongString"]=1.0;
m["anotherLongString"]=2.0;
//
// now steal resources
for (auto &p : m) {
// according to my IDE, p has type
// std::pair<const …
Run Code Online (Sandbox Code Playgroud) 在以下C++代码中,该行bar<func_ptr>(); //does not work
会导致编译错误:
#include <iostream>
using namespace std;
void foo(){
cout<<"Hello world";
};
template<void(*func)()>
void bar(){
(*func)();
}
int main() {
using fun_ptr_type= void(*)();
constexpr fun_ptr_type func_ptr=&foo;
bar<&foo>(); //works
bar<func_ptr>(); //does not work
return 0;
}
Run Code Online (Sandbox Code Playgroud)
g ++的输出是这样的:
src/main.cpp: In function ‘int main()’:
src/main.cpp:19:16: error: no matching function for call to ‘bar()’
bar<func_ptr>(); //does not work
^
src/main.cpp:10:6: note: candidate: template<void (* func)()> void bar()
void bar(){
^~~
src/main.cpp:10:6: note: template argument deduction/substitution failed:
src/main.cpp:19:16: error: ‘(fun_ptr_type)func_ptr’ …
Run Code Online (Sandbox Code Playgroud) 以下代码只能在 GCC 上编译(在 godbolt.org 上检查了 10.4 和 13.2),但不能在 Clang 上编译(在我尝试过的所有版本上都失败,例如 godbolt.org 上的 17.0.1):
struct A {
static constexpr int b{1};
};
int main(int argc, char *argv[]) {
A a;
A& aref{a};
constexpr auto bb1{a.b};
constexpr auto bb2{aref.b};
return bb1+bb2;
}
Run Code Online (Sandbox Code Playgroud)
叮当输出:
struct A {
static constexpr int b{1};
};
int main(int argc, char *argv[]) {
A a;
A& aref{a};
constexpr auto bb1{a.b};
constexpr auto bb2{aref.b};
return bb1+bb2;
}
Run Code Online (Sandbox Code Playgroud)
https://godbolt.org/z/nG4j3KefE
为什么?
我偶然发现我可以const std::pair<const int, int>&
参考std::pair<int,int>
:
#include <utility>
int main()
{
std::pair<int, int> p{1,2};
const std::pair<const int, int>& ref_ok{p}; // why does this work?
std::pair<const int, int>& ref_invalid{p}; // then why does this not work?
}
Run Code Online (Sandbox Code Playgroud)
考虑到const std::pair<const int, int>
和std::pair<int, int>
是不通过继承相关的不同类型,为什么这是可能的?
std::array<uint8_t,N>
我正在尝试从 a创建 a,std::span<uint8_t,N>
但我找不到一种没有memcpy
、或 的方法来执行此操作std::copy
,或者std::ranges::copy
这不能保护我免受目标数组大小的错误指定。
#include <algorithm>
#include <array>
#include <iostream>
#include <span>
int main(int argc, char **argv) {
constexpr size_t N = 10;
std::array<uint8_t, N> original;
std::span span(original); // of type std::span<uint8,N>
std::array copy1(span); // does not work
std::array<uint8_t, N> copy2(span); // does not work
std::array<uint8_t, N> copy3(begin(span), end(span)); // does not work
// ugly stuff that works, but does not protect me if I specify wrong array size
constexpr size_t …
Run Code Online (Sandbox Code Playgroud) 我有两个结构A1
,A2
另一个结构B
有两个结构的构造函数。如何编写 的委托构造函数std::variant<A1,A2>
?
#include <variant>
struct A1 {};
struct A2 {};
using A = std::variant<A1, A2>;
struct B {
B(const A1 &){}
B(const A2 &){}
B(const A& a)
:B(a) // <--- what do I put here to delegate like std::visit ?
{}
};
int main(int argc, char *argv[]) {
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种本着 的精神的干净的现代方式std::visit
。B
我知道我可以使用静态成员函数通过A
using和 lambda构造实例std::visit
,但这不是我所要求的,因为我想依赖从A
到 的隐式类型转换B
。
Julia中的卷积函数具有以下行为:
_
_ _ _(_)_ | A fresh approach to technical computing
(_) | (_) (_) | Documentation: https://docs.julialang.org
_ _ _| |_ __ _ | Type "?help" for help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 0.6.1 (2017-10-24 22:15 UTC)
_/ |\__'_|_|_|\__'_| | Official http://julialang.org/ release
|__/ | x86_64-pc-linux-gnu
julia> conv([1,2,NaN],[1])
3-element Array{Float64,1}:
NaN
NaN
NaN
Run Code Online (Sandbox Code Playgroud)
这是一个错误吗?或者这与使用的FFT算法有关conv
?
如果其中一个向量包含NaN
s,我如何在Julia中计算卷积(不是出于性能原因而手动)?在此示例中,结果应为:
3-element Array{Float64,1}:
1.0
2.0 …
Run Code Online (Sandbox Code Playgroud) 我有来自mysql.com的以下代码:
/* Copyright 2008, 2010, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
There are special exceptions to the terms and conditions of the GPL
as it is applied to this software. View the full text of the
exception in file EXCEPTIONS-CONNECTOR-C++ in the directory of …
Run Code Online (Sandbox Code Playgroud) 考虑两个不同大小的矩阵
\njulia> A= ones(Float64,3,3)\n3\xc3\x973 Matrix{Float64}:\n 1.0 1.0 1.0\n 1.0 1.0 1.0\n 1.0 1.0 1.0\n\njulia> B = ones(Float64,2,2)\n2\xc3\x972 Matrix{Float64}:\n 1.0 1.0\n 1.0 1.0\n
Run Code Online (Sandbox Code Playgroud)\n有没有简写
\njulia> A[1:size(B)[1],1:size(B)[2]]\n2\xc3\x972 Matrix{Float64}:\n 1.0 1.0\n 1.0 1.0\n
Run Code Online (Sandbox Code Playgroud)\n以像“”这样的好函数的形式julia> A[indices(B)]
(不幸的是它不存在ERROR: UndefVarError: indices not defined
)?
考虑以下代码:
#include<iostream>
using namespace std;
struct A {
A(const A&){
cout<<"copy constructor"<<endl;
}
A(A&&){
cout<<"move constructor"<<endl;
}
~A(){
cout<<"destructor"<<endl;
}
static std::pair<A,int> f1()
{
int i = 1;
return std::pair<A,int>{i,2};
}
static std::pair<A,int> f2()
{
int i = 1;
return std::pair<A,int>{A(i),2};
}
private:
A(int){
cout<<"constructor"<<endl;
}
};
int main()
{
cout<<"f1: "<<endl;
A::f1();
cout<<"f2: "<<endl;
A::f2();
}
Run Code Online (Sandbox Code Playgroud)
构造函数A(int)
是私有的,因此A
不能pair<A,int>
从 就地构造int
。因此,在 中构建了一个临时的f1
。在f2
我显式创建临时文件但行为不同时,输出为:
f1:
constructor
copy constructor
destructor
destructor
f2:
constructor …
Run Code Online (Sandbox Code Playgroud) c++ ×8
julia ×2
c++20 ×1
clang ×1
constexpr ×1
constructor ×1
convolution ×1
exception ×1
move ×1
reference ×1
std-pair ×1
std-span ×1
std-variant ×1
stdarray ×1
templates ×1