请考虑以下代码:
struct Foo
{
mutable int m;
template<int Foo::* member>
void change_member() const {
this->*member = 12; // Error: you cannot assign to a variable that is const
}
void g() const {
change_member<&Foo::m>();
}
};
Run Code Online (Sandbox Code Playgroud)
编译器生成错误消息.问题是成员m是可变的,因此可以改变m.但是函数签名隐藏了可变的声明.
如何对指向mutable-member的decalre来编译这段代码?如果不可能,请链接到标准C++.
我有一个用于收集函数参数的野牛语法。到目前为止,情况是这样的:
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) 在使用首次可变矢量,我发现,虽然Data.Vector.Unboxed拥有所有你喜欢预期更高层次的功能map,fold等等,可变的版本Data.Vector.Unboxed.Mutable没有这些。尝试在可变向量上使用 immutable 包中的函数不起作用。
mutable vector 包缺少很多这些高级函数的原因是什么?
我收到“变异运算符的左侧不可变”..<“返回不可变值”错误
我阅读了关于变异值的另一篇文章,但我无法弄清楚这些解决方案是如何应用的。
代码(和评论):
//populate array of 3 random numbers using correct answer and 2 incorrect choices
func insertIntoArray3(_ randomNumber: Int) -> Int {
for intJ in 0 ..< 2 += 1{
if arrayIndex != 3 {
checkIfExists(randomNumber)
if ifExists {
let randomNumber = 1 + random() % 10
insertIntoArray3(randomNumber)
} else {
array3[arrayIndex] = (randomNumber)
arrayIndex = arrayIndex + 1
}
}
}
return randomNumber
}
Run Code Online (Sandbox Code Playgroud)
修订代码:
//populate array of 3 random numbers using correct answer and 2 incorrect choices …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 …我试图递归一个节点结构,修改它们,然后返回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) 我有这个:
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/ )