lifetime Questions

8

Solved

Unlike new and delete expressions, std::malloc does not call the constructor when memory for an object is allocated. In that case, how must we create an object so that the constructor will also be ...
Hindustan asked 8/6, 2010 at 6:2

1

Solved

I have the following data structure (simplified): use std::collections::HashMap; pub struct StringCache { // the hashmap keys point to elements stored in `storage` // so never outlive this datast...
Necrophobia asked 30/5, 2023 at 12:11

3

Solved

The docs for slice::from_raw_parts warn the programmer to annotate the slice with the correct lifetime. I assume that, given some lifetime 'a, I can perform this annotation with let myslice: &amp...
Jeopardy asked 23/10, 2015 at 15:2

1

I'm trying to understand lifetime extension guarantees in C++. Can someone explain why the usage of different types of parentheses below yields differing results in terms of when the temporar...
Lonna asked 24/4, 2023 at 2:11

4

I stumbled upon the following code snippet: #include <iostream> #include <string> using namespace std; class First { string *s; public: First() { s = new string("Text");} ~First() ...

3

Solved

I have the following Zip class in C++, which works the same as Python's zip. When I run the below code, I get this output: 1 | 11 2 | 22 3 | 33 1 | 11 | 0 <--- problematic 2 | 22 | 6.91092e-317...
Shrum asked 23/3, 2023 at 10:18

1

Solved

According to this and this an aggregate is implicit lifetime. A class S is an implicit-lifetime class if it is an aggregate or has at least one trivial eligible constructor and a trivial, non-dele...
Oxbridge asked 11/3, 2023 at 20:48

2

Solved

Can someone explain the execution order of this code? struct Foo { ~Foo() { std::cout << "1"; } }; int main() { const Foo& bar = Foo(); const Foo& baz = std::move(Foo(...
Tension asked 7/2, 2023 at 10:56

2

Solved

Consider the following code: struct Temp{ int i = 0; }; Temp GetTemp() { return Temp{}; } int main() { for (struct{Temp const & t; int counter;} v = {GetTemp(), 0}; v.counter < 10; ++v...
Vintner asked 20/1, 2023 at 12:59

2

Solved

Consider the following Rust code: use std::thread; fn main() { bar(); loop {} } fn bar() { let var = b"foo"; thread::spawn(|| { write(var); }); } fn write(d: &[u8]) { printl...
Larousse asked 17/1, 2023 at 19:21

4

The following Rust code compiles successfully: struct StructNothing; impl<'a> StructNothing { fn nothing(&'a mut self) -> () {} fn twice_nothing(&'a mut self) -> () { self...
Glomeration asked 4/10, 2014 at 11:55

1

Solved

I have the following two structs for which I derive serde traits. use serde::{Serialize, Deserialize}; #[derive(Serialize, Deserialize)] pub struct Item<'a> { pub id: &'a str, pub name...
Aback asked 21/11, 2022 at 10:7

1

Solved

I'm having an issue with lifetimes that I'm not sure how to solve, since it seems like the change I'm making should be trivial with regards to lifetimes. Given: use anyhow::Context; use nom::{IResu...
Chromous asked 26/8, 2022 at 16:14

1

Solved

I'm attempting to build a sort of HTTP web server as a learning exercise and I'm having trouble trying to make one of my types iterable using a for loop (by implementing IntoIterator). So far I've ...
Handedness asked 23/8, 2022 at 18:19

4

The following error goes away if I do as rustc tells me to, and change the bound to where F: Fn() -> () + 'static pub struct Struct(Box<dyn Fn() -> ()>); pub fn example<F>(f: F) w...
Inclined asked 26/12, 2021 at 19:54

1

Solved

I have a function that expects a short lived object. I would expect that I would be able to always pass it a long lived object. But I am getting a strange error when I try to encode that: type F&lt...
Hydrokinetic asked 18/8, 2022 at 19:8

4

Solved

I have a value and I want to store that value and a reference to something inside that value in my own type: struct Thing { count: u32, } struct Combined<'a>(Thing, &'a u32); fn make_...
Humbug asked 30/8, 2015 at 19:6

1

Solved

This is very similar to Correct usage of placement-new and explicit destructor call but tighter in scope: If I have a type, S, for which std::is_nothrow_default_constructible_v<S> and std::is...
Nilotic asked 7/7, 2022 at 18:38

1

Code Playground (Stable Rust 1.45.0, 2018 edition) No external crates needed for example. type Error = Box<dyn std::error::Error>; type Result<R=()> = std::result::Result<R, Error&gt...
Tiein asked 26/7, 2020 at 2:26

5

I want to write a program that will write a file in 2 steps. It is likely that the file may not exist before the program is run. The filename is fixed. The problem is that OpenOptions.new().write(...
Rabat asked 20/9, 2015 at 18:34

2

Solved

This code won't compile because rust requires a lifetime to be added. fn firstNoLifetime(x: &str, y: &str) -> &str { return x; } So instead we must add the lifetime explicitly like...
Krahling asked 3/4, 2022 at 2:58

3

Solved

I thought references only extend the lifetime of temporaries to the lifetime of the reference itself, but the output of the following snippet seems contradictory: #include <iostream> struct...
Jaques asked 23/9, 2012 at 17:38

1

Solved

Let's say that I have the following working code: use std::collections::VecDeque; fn modify<S, F>(state: &mut S, func: F) where F: for<'a> Fn(&'a mut S) -> Box<dyn Itera...
Vouvray asked 8/2, 2022 at 10:3

1

Solved

New to Rust and trying to teach myself, etc. I'm stuck on a lifetime issue. The closest question I could find that was already posted was: Argument requires that _ is borrowed for 'static - how...
Pistol asked 11/1, 2022 at 21:12

2

Solved

In Salsa, there is a higher-ranked trait bound on a trait. I've seen HRTBs on function definitions but not on a trait. What does it mean? pub trait Query: Debug + Default + Sized + for<'d> Qu...
Santonin asked 2/1, 2022 at 16:23

© 2022 - 2024 — McMap. All rights reserved.