17,328 questions
4
votes
3
answers
204
views
Reference Types: Variable Not Initialized Error Confusion
My instructor gave us this question for homework:
11. Select the TRUE statement.
a) This code will not compile.The error will be:
local variable c may not have been initialized
b) ...
Advice
3
votes
4
replies
258
views
Using a C++ const reference from a return value
I have trouble understanding the C++ documentation. Will this cause a dangling pointer, or is it safe to do? I don't trust AI-generated information.
#include <iostream>
#include <vector>
#...
2
votes
2
answers
169
views
How to simplify checking whether a reference variable is null?
I wrote a method that returns a reference to a variable (ref return), and sometimes the returned reference can be null. After calling this method I have to check if the reference is null, and I am ...
2
votes
2
answers
178
views
What are exactly structured bindings?
I was surprised to get a deprecation warning from google benchmark function DoNotOptimize when calling it on a structured binding:
benchmark::DoNotOptimize(const Tp&) [with Tp = int; typename std:...
3
votes
1
answer
124
views
how to get update-able sheet name in google sheets
is there a way how to get updateable sheet name of the active or any other sheet without using scripts - only native formulae allowed and no hardcoding
for example, in the spreadsheet there is a sheet ...
1
vote
0
answers
53
views
C# Maui passing a reference of 'this' down a code chain in order to use a DisplayAlert, does not display the alert, but awaits a result [duplicate]
As I build a datagrid with elements that each have a command associated with them based on a template, I realized that I can't just have a DisplayAlert on the buttons that execute a command. I ...
3
votes
2
answers
271
views
"Transparent" function: perfectly forwarding an argument to the return value
I'm trying to figure out how the signature of a completely transparent function might look. That is, a function that takes an argument T and that itself (i.e. from its return value) then looks exactly ...
2
votes
1
answer
95
views
Allowing Access to Fields in Inner Struct with Different Names in Rust
I am trying to allow for the interface in an outer struct to allow using fields that it does not hold representing information of an inner struct. I will explain with an example because reading it ...
1
vote
1
answer
63
views
How to pass a std::thread::scope as a parameter with another reference
This works fine:
fn main() {
test1();
}
fn test1() {
let data = "data".to_string();
std::thread::scope(|scope| {
scope.spawn(move || {
println!("print ...
-1
votes
2
answers
191
views
Declaring member variables in a class interface header file
Having a Java and C background, I am currently learning C++ with an old but good book which explains that all members of a class including variables have to be declared but not initialized in its ...
5
votes
1
answer
198
views
std::copy_assignable_v not working for reference types, how to fix?
Consider the following code where I test whether a type is copy-assignable by using the std::is_copy_assignable_v type-trait:
#include <print>
#include <type_traits>
struct IntProxy
{
...
4
votes
3
answers
374
views
Passing a local reference to a closure
Consider the code below:
for (T &t : some_vector_) {
futures.push_back(thread_pool.submit([&t] { t.do_something(); }));
}
On some iteration i, t is initialized to a reference to a vector ...
Best practices
0
votes
2
replies
108
views
How should I store a dyn FnMut in a struct?
I'm trying to make a struct that has dynamic methods by storing FnMuts in the struct. The FnMuts would be a function written in a scripting language like Python or Lua, but there might be some cases ...
1
vote
1
answer
275
views
Loss of reference
I am trying to create an event emitter similar to NodeJS:
emitter.cc
#include "event/emitter.h"
namespace game {
void Emitter::on(std::string eventName, Listener *listener) {
std::list&...
1
vote
1
answer
84
views
Question about reference of permission Read and Own
From references-change-permissions-on-places, places have three kinds of permission on their data:
Read (R): data can be copied to another location.
Write (W): data can be mutated.
Own (O): data ...
1
vote
1
answer
54
views
I am trying to use the value in a cell as an address to paste to another cell in google sheets via apps scripts. Use cell F8 which = C6 to paste to C6
I would like to paste from one tab to the address of a cell in another tab, where the value changes.
The problem I'm trying to solve is: how do I write the Apps Script code so that:
var spreadsheet = ...
3
votes
1
answer
166
views
Conditional operator with a throwing branch in Visual C++ 2026
My program changed its behavior after updating Visual Studio to the latest version (2026).
Simplifying it, I got the following minimal example, which contains a ternary operator with a throw in ...
3
votes
1
answer
230
views
Use decltype(auto) return type to sometimes return references
I have the following code to normalize std::filesystem::path to always use forward slashes as separators:
static decltype(auto) fix_path_separator(const std::filesystem::path& path) {
if ...
1
vote
1
answer
111
views
Use a reference to $a to make $a a reference to $b
I have an array $arr = ['a', ['b',['c','d','e']]];.
So $arr[1][1][0] is 'c'.
I have these indexes listed in n array: $idx = [1,1,0];
(this may sound tricky, but comes from the fact that I got them ...
0
votes
1
answer
87
views
How to assign a new object to a reference?
Why can't I do it like this in Go?
func do(m1 map[int]string) {
var m map[int]string = make(map[int]string)
*m1 = &m;
}
I have m1 map, which means I can now it's reference? How to assign ...
1
vote
1
answer
161
views
Avoiding double references in Rust
I have the following utility method to get raw bytes of an object (to send via network, etc.):
pub fn to_bytes<T>(data: &T) -> &[u8] {
unsafe { slice::from_raw_parts((data as *...
5
votes
1
answer
194
views
Segmentation fault when trying to assign a global variable to a static reference variable
The following program gives segmentation fault.
#include <iostream>
using namespace std;
class A {
public:
static int& a;
};
int a = 42;
int& A::a = a;
int main() {
a = 100;
...
2
votes
1
answer
190
views
c++20 tuple compare with reference
We upgrade our codebase from c++17 to c++20 lately, and some code does not work as before. I take the following simplified sample code as show case here.
None of the overloaded compare operators is ...
1
vote
2
answers
123
views
Creating virtual object in constructor initialization
I want to create a variable of type Foo. The class Foo consists of a variable called Bar bar_ which is filled directly in the constructor initialization. The thing is, class Bar has a reference to the ...
1
vote
1
answer
95
views
Simplifying getting a specific type from an enum
I have a set of classes linked into an enum:
#[derive(Default)]
pub struct A {
}
#[derive(Default)]
pub struct B {
}
#[derive(Default)]
pub struct C {
}
enum Classes {
A(A),
B(B),
C(C),
...
5
votes
2
answers
321
views
Can an rvalue of a specific class type be automatically cast to an lvalue of itself?
In C++, a function with signature void f(A& a) (non-template) only binds to a lvalue.
Is it possible for an rvalue to automatically cast itself to an lvalue?
This is what I tried:
#include <...
12
votes
1
answer
309
views
C++ temporary objects and constant reference
I am going through "C++ Primer", and it is mentioned that for the following code:
double dval = 3.14;
const int &ri = dval; //Bind a const int to a plain double object.
The ...
0
votes
1
answer
91
views
How to reference a second Pandas dataframe to the first one without creating any copy of the first one?
I have a large pandas dataframe df of something like a million rows and 100 columns, and I have to create a second dataframe df_n, same size as the first one. Several rows and columns of df_n will be ...
28
votes
1
answer
2k
views
Why is `&self` allowed in the parameters of `&mut self` methods, but not `&mut self`?
Rust method calls are desugared to fully-qualified-syntax and function parameters are evaluated left to right. Why then does s.by_mut(s.by_ref(())) compile while other
expressions don't?
struct S;
...
0
votes
2
answers
114
views
How can the italicized text be referenced in other cells?
I asked a question about italicizing specific parts of a cell (Making variable strings of text italics in Excel VBA).
I run the macro in column N to get what I want italicized, but when I reference ...
3
votes
1
answer
143
views
pybind11: Python callback executed in C++ with parameter modification
I'm working on Python bindings of my C++ library (a mathematical optimization solver) and I'm stuck at a point where I create a Python callback evaluate_constraints() that takes two arguments, pass it ...
1
vote
1
answer
88
views
What are the basic rules for propagating lists and dictionaries across processes using the Manager of the multiprocessing module?
I am trying to use the multiprocessing module to parallelize a CPU-intensive piece code over multiple cores. This module looks terrific in several respects, but when I try to pass lists and ...
1
vote
1
answer
108
views
Any way to create a bitfield reference wrapper in a generic way?
I am creating wrappers for C structs containing register definitions with heavy use of bitfields. I would like to create reference getters for all of them, to provide consistent shorthand API (real ...
0
votes
0
answers
118
views
Lost name reference when setting extra attribute
I have shot myself in the foot a fair number of times with declare until I started to differentiate its switches that are not all equal. Some set variable attributes, but some just affect scope. So ...
0
votes
0
answers
44
views
VisualStudio 2022 CodeLens show references changed by itself
I've just noticed that my alt+2 hotkey for opening CodeLens show references window I've been using for years changed to alt+3. This happened only on my PC, on my laptop it's still on alt+2. There is ...
0
votes
1
answer
188
views
References name conflict across functions?
There's some interesting shenanigans going on with references:
#!/bin/bash
read -r -d '' payload << EOF
apple: vinegar
orange: juice
EOF
populate() {
declare -n _aa=$1
declare data=&...
-1
votes
2
answers
220
views
How to deserialize ${aa[@]@K} into referenced associative array with no eval effect?
How can I reference an associative array and reassign its content from serialized string?
(The eval requirement is now spelled out explicitly in the title, but this is natural when it comes to ...
2
votes
1
answer
95
views
Reference an open Excel file from PowerPoint using VBA [duplicate]
I found the following VBA code for PowerPoint which works and allows PowerPoint to reference an Excel file
Private Sub test()
Dim xlApp As Object
Dim xlWorkBook As Object
Set xlApp = CreateObject(&...
-5
votes
1
answer
80
views
What’s the mechanism that makes destructuring & rest parameter create an isolated copy, while rest parameter expose the original array to mutation?
function t(..._a)
{
console.log("t says:", _a[0] === a); // Output: t says: true
}
function v([..._a])
{
console.log("v says:", _a === a); // Output: v says: ...
0
votes
2
answers
150
views
Behavior is different when using the input "reference" variable, compared to when the same reference is copied
The Minimal Example that reproduces the issue is as follows:
#include <iostream>
#include <string>
#include <list>
#include <iterator>
#include <vector>
#include <...
1
vote
0
answers
18
views
Will passing an HTML reference to a React Custom Hook cause performance issues if the hook runs a lot? [duplicate]
I understand I can pass a reference to an HTML element to my React custom hook in react such as following:
let myCustomHook(elem:React.RefObject<any>) {
// logic
}
However, passing by reference ...
3
votes
1
answer
341
views
Why can a mutable reference variable be mutated-through even when it itself is not declared mut?
I have recently started learning Rust from The Book.
While learning about ownership, I came up with following piece of code:
let mut s = String::from("hello");
let r1 = &mut s;
Here it ...
3
votes
1
answer
131
views
When comparing a value and a reference, should I dereference the reference or create a reference to the value?
I'm trying to learn Rust and I managed to find two ways to solve a question (which was to convert from celsius to fahrenheit).
fn convert_only_if_needed(celsius_temp : &f64) -> f64 {
if !(*...
0
votes
1
answer
172
views
Delphi, referenching a TabSheet from another Tabsheet of a pagecontrol
On my main form (frmMain) I host a (Konopka) PageControl (TRzPageControl named pgcMain) with a tabsheet created at design time (pgDashboard). I created at frmMain.Create a dynamic form frm : ...
1
vote
0
answers
46
views
Do non-static fields of a const lvalue reference type extend the lifetime of a temporary? [duplicate]
Initially, to me the answer was obvious. However, I've come across an answer explaining binding temporaries to constant lvalue references which states the following:
This only applies to stack-based ...
1
vote
2
answers
119
views
What is the semantic difference in Rust between defining and initializing an instance of a structure and a reference to structure?
What is the semantic difference between defining and initializing an instance of a structure and a reference to structure? In other words, what's the difference in Rust between these two programs?
...
3
votes
2
answers
219
views
How to understand "constness is shallow" with regard to references (as stated in cppreference.com)?
While studying the cppreference spec of std::atomic_ref, I found myself brooding over the following sentence:
Like references in the core language, constness is shallow for std::atomic_ref - it is ...
0
votes
1
answer
84
views
Unresolved reference: ViewModelScope
I'm trying to run the example program at https://developer.android.com/kotlin/coroutines#examples-overview in Android Studio Iguana:
import androidx.lifecycle.ViewModel
import kotlinx.coroutines....
1
vote
1
answer
101
views
Is an FnMut argument a reference?
I've been trying to understand closures better. I originally wrote
fn main() {
let mut x = 0;
let f = || x += 1;
call_me(f);
f();
println!("x = {x}");
}
fn call_me<F&...
2
votes
2
answers
2k
views
Is it possible to reference .NET Framework 4.8 libraries in a .NET 8 project?
I'm working with an older C# solution built on .NET Framework 4.8, which contains several libraries. Now, I'm developing a new solution targeting .NET 8 that needs to reuse some of those existing ...