我有一个用于收集函数参数的野牛语法。到目前为止,情况是这样的:
args: arg {$$ = intArray($1);} //pseudo-code function
| args arg {$$ = $1 + $2;} //pseudo-code array addition
arg : NUM {$$ = $1;}
| exp {$$ = $1;}
Run Code Online (Sandbox Code Playgroud)
如何创建一个没有固定长度的整数数组,可以像 Bison 中的普通 C 数组一样使用?
出于好奇,
是否有可能派生出父成员mutable?有没有合理的方法修改以下代码并使其编译?(保留foo()方法const而int a不是mutable在父类中声明时保留方法A)
class A {
protected:
int a;
};
class B : public A {
public:
void foo() const {
a = 10;
}
};
Run Code Online (Sandbox Code Playgroud) 我有一个HashMap<i8, i8>可以包含循环的:
let mut x: HashMap<i8, i8> = HashMap::new();
x.insert(1, 6);
x.insert(3, 5);
x.insert(5, 1);
Run Code Online (Sandbox Code Playgroud)
要获得 的最终值3,应该首先查找x[3],然后x[5]最后查找x[1]哪个应该产生6。我决定使用while let循环:
let mut y = x[&3]; // y: i8
while let Some(&z) = x.get(&y) {
y = z;
}
println!("{}", y);
x.insert(0, 0);
Run Code Online (Sandbox Code Playgroud)
这工作正常,但panic!如果3不在地图上,它就会工作。由于我不想对这种None情况做任何事情,所以我想使用 a if let(类似于while let使用的)。
我尝试过一些符号:
if let Some(&y) = x.get(&3):复制值,但 y 不可变 ( y: i8 …如果我像这样使用指针mutable:const
class Test
{
public:
mutable const int* ptr; // OK
};
Run Code Online (Sandbox Code Playgroud)
运行良好。
但是,如果我这样使用:
class Test
{
public:
mutable int * const ptr; // Error
};
Run Code Online (Sandbox Code Playgroud)
一个错误 :
prog.cpp:6:25: error: const 'ptr' cannot be declared 'mutable'
mutable int * const ptr;
^
prog.cpp: In function 'int main()':
prog.cpp:11:7: error: use of deleted function 'Test::Test()'
Test t;
^
prog.cpp:3:7: note: 'Test::Test()' is implicitly deleted because the default definition would be ill-formed:
class Test
^
prog.cpp:3:7: error: uninitialized const …Run Code Online (Sandbox Code Playgroud) 我试图递归一个节点结构,修改它们,然后返回Node我得到的最后一个。我使用非词法生命周期 RFC 中的示例解决了循环中可变引用的问题。如果我尝试将可变引用返回到 last Node,则会出现use of moved value错误:
#[derive(Debug)]
struct Node {
children: Vec<Node>,
}
impl Node {
fn new(children: Vec<Self>) -> Self {
Self { children }
}
fn get_last(&mut self) -> Option<&mut Node> {
self.children.last_mut()
}
}
fn main() {
let mut root = Node::new(vec![Node::new(vec![])]);
let current = &mut root;
println!("Final: {:?}", get_last(current));
}
fn get_last(mut current: &mut Node) -> &mut Node {
loop {
let temp = current; …Run Code Online (Sandbox Code Playgroud) 示例代码?
fn main() {
let a = [1, 2, 3, 4, 5];
reset(a);
}
fn reset(mut b: [u32; 5]) {
b[0] = 5;
}
Run Code Online (Sandbox Code Playgroud)
变量a是一个不可变数组,reset函数的参数b是一个可变数组;直觉上我需要修改a为可变数组才能调用该reset方法,但是编译器告诉我不需要这样做,这是为什么呢?
fn main() {
let mut a = [1, 2, 3, 4, 5];
reset(a);
}
fn reset(mut b: [u32; 5]) {
b[0] = 5;
}
Run Code Online (Sandbox Code Playgroud)
fn main() {
let a = [1, 2, 3, 4, 5];
reset(a);
}
fn reset(mut b: [u32; 5]) {
b[0] = …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 kotlin 创建一个 MutableList 但我收到一条错误消息:
类型推断失败。预期类型不匹配:推断类型为 MutableList 但预期为 MutableCollection
...而且我不确定如何将 MutableList 转换为 MutableCollection。
我试过使用:
.toMutableList().toCollection()
Run Code Online (Sandbox Code Playgroud)
但它正在寻找目的地——我不知道该怎么做。
代码片段:
data class HrmSearchResult(
var rssi: Short?,
var adjustRssi: Short?,
var timeout: Int,
var serialNumber: Long?,
var isIn: Boolean,
var countIn: Int
)
private val hashMapHrm = ConcurrentHashMap<Long?, HrmSearchResult>()
val hrmDeviceList: MutableCollection<Long>
get() = try {
if (hashMapHrm.elements().toList().none { it.isIn}) {
//if there are no member in range, then return empty list
arrayListOf()
} else {
hashMapHrm.elements()
.toList()
.filter { it.isIn }
.sortedByDescending { …Run Code Online (Sandbox Code Playgroud) 我有这个:
let alphaPtr = UnsafeMutablePointer<vImagePixelCount>(mutating: alpha) as UnsafeMutablePointer<vImagePixelCount>?
Run Code Online (Sandbox Code Playgroud)
现在我收到警告:
“UnsafeMutablePointer”(又名“UnsafeMutablePointer”)的初始化导致悬空指针
详细警告包括:
从'[vImagePixelCount]'(又名'Array')到'UnsafePointer'(又名'UnsafePointer')的隐式参数转换会产生一个指针,该指针仅在对'init(mutating:)'的调用期间有效
在 Array 上使用“withUnsafeBufferPointer”方法,以便将参数显式转换为对定义的范围有效的缓冲区指针
有没有解决的办法?
我正在编写一些像 stl 这样的自定义库,但在 ctors 中没有分配,并且在资源拥有类中禁用了复制 ctors(因为环境不支持异常,堆上的所有分配都需要通过 retcode 检查)。
所以我从https://github.com/Kronuz/cpp-btree/移植 btree ,并且由于代码问题和我的方法一起被卡住了。
value_type与所有 stl 实现一样,是std::pair< const Key, Value>。const限定符使整个对隐式不可移动。
所以代码
x->construct_value(j, std::move(fields_.values[i]));
Run Code Online (Sandbox Code Playgroud)
( https://github.com/Kronuz/cpp-btree/blob/35ac0ec96f1fca1463765f169390059ab82d3aac/btree/btree.h#L615 )
实际上不会移动对象(返回 T& 而不是 T&&)并且
new (v) value_type(std::forward<Args>(args)...);
Run Code Online (Sandbox Code Playgroud)
( https://github.com/Kronuz/cpp-btree/blob/35ac0ec96f1fca1463765f169390059ab82d3aac/btree/btree.h#L883 )
正确地无法通过复制构造函数构造对。
有没有办法在没有或绕过复制移动语义的情况下在内存中重定位对象?当然,简单的解决方法是使std::pair< Key , Value>具有可变键,但这并不完全相同。
我找到了 Arthur O'Dwyer 提出的“trivially_realocable”提议,我得出的结论是,这正是关于这种情况的。( https://quuxplusone.github.io/blog/2018/07/18/annoucing-trivially-relocatable/ )
我正在尝试创建一个由函数 f(x) 组成 N 次的函数,类似于:
function CompositionN(f,N)
for i in 1:N
f(x) = f(f(x))
end
return f(x)
Run Code Online (Sandbox Code Playgroud)
我需要函数 CompositionN 返回另一个函数,而不是值。
mutable ×10
c++ ×3
rust ×3
pointers ×2
android ×1
arrays ×1
bison ×1
borrowing ×1
c ×1
collections ×1
composition ×1
constants ×1
flex-lexer ×1
immutability ×1
inheritance ×1
ios ×1
julia ×1
kotlin ×1
move ×1
reference ×1
repeat ×1
swift ×1
syntax ×1
variables ×1
xcode ×1