argument-passing

Will a good C++ compiler optimize a reference away?

家住魔仙堡 提交于 2021-02-18 20:08:48
问题 I want to write a template function that does something with a std::stack<T> and an instance of T , e.g.: template<class StackType> inline bool some_func( StackType const &s, typename StackType::value_type const &v ) { // ... } The reason I pass v by reference is of course to optimize for the case where StackType::value_type is a struct or class and not copy an entire object by value. However, if StackType::value_type is a "simple" type like int , then it's of course better simply to pass it

Will a good C++ compiler optimize a reference away?

梦想的初衷 提交于 2021-02-18 20:06:36
问题 I want to write a template function that does something with a std::stack<T> and an instance of T , e.g.: template<class StackType> inline bool some_func( StackType const &s, typename StackType::value_type const &v ) { // ... } The reason I pass v by reference is of course to optimize for the case where StackType::value_type is a struct or class and not copy an entire object by value. However, if StackType::value_type is a "simple" type like int , then it's of course better simply to pass it

Functional-style JavaScript: good practice to avoid argument mutation?

风流意气都作罢 提交于 2021-02-18 11:16:11
问题 This is a rather general question. Functional-style programming promotes the idea that a program is about transforming data through functions, and that mutation should be avoided (except possibly within a function, seen as a basic unit of abstraction). But in this program: function foo (bar) { bar.k1 = "bananas"; return bar; } var o = { k1: "apples", k2: "oranges"}; var p = foo(o); the external variable o is mutated within foo because bar is a reference to o, and, in the end, o === p (they

Passing arguments to python eval()

给你一囗甜甜゛ 提交于 2021-02-11 06:59:42
问题 I'm doing genetic programming framework and I need to be able to execute some string representing complete python programs. I'm using Python 2.7. I have a config class in which the primitive sets are defined. Lets say class Foo(): def a(self,x): return x def b(self,y): return y I'm extracting the functions with the python inspect module and I want to create some executable source code with imports and everything. I end up with a string that looks like this import sys def a(x,y): return x def

How to P/Invoke “__arglist” function?

别说谁变了你拦得住时间么 提交于 2021-02-10 15:11:30
问题 Background: I have written the following function in C++: extern "C" __declspec(dllexport) int test(const char*, ...); And I am using P/Invoke to call it from C#: [DllImport("foo/bar.dll", EntryPoint = "test")] public static unsafe extern int __unmanaged__test(byte* buffer, __arglist); Problem: I need to initialize the __arglist dynamically, meaning that I have an Array<object> , which I have to convert into the __arglist before calling the C++-function. I have tried the following, but it

conditional inclusion of arguments in a function call

余生长醉 提交于 2021-02-07 05:54:45
问题 I want to call a function but depending on the situation I might call it with extra arguments or not. Here is a simple example: FUN <- function(arg1 = "default1", arg2 = "default2", arg3 = "default3") print(list(arg1, arg2, arg3)) x1 <- "hi" x2 <- TRUE x3 <- 1:3 use.arg3 <- FALSE # This will decide if `x3` is used or not. if (use.arg3) { FUN(arg1 = x1, arg2 = x2, arg3 = x3) } else { FUN(arg1 = x1, arg2 = x2) } While the code is clear, it feels a little redundant. Also imagine that if I had

python: pass multiple arguments from one function to another

≯℡__Kan透↙ 提交于 2021-02-04 15:17:34
问题 I'm trying to learn python (with my VBA background) buy building a black-jack game as a pedagogical exercise. I've done some searches about passing multiple arguments but I really don't understand what i'm finding in the way of explanations. Looking at the last function called 'hand' i'm trying to make use of three separate values passed as a 'return' from a previous function. I get the following error: Traceback (most recent call last): File "decky15.py", line 56, in <module> print hand(deal

The representation of an empty argument in a “call”

懵懂的女人 提交于 2020-08-17 11:55:32
问题 What kind of animal is an empty argument? Consider the following piece of code. > f <- function(...) match.call() > l <- as.list(f(,3)) > l [[1]] f [[2]] [[3]] [1] 3 > typeof(l[[2]]) [1] "symbol" > identical(l[[2]],``) Error: attempt to use zero-length variable name > as.character(l[[2]]) [1] "" What sorcery is that? 回答1: If looks like l[[2]] 's value is a special object: the empty symbol. Some facts about the empty symbol (speculated based on casual experimentation). (1) The empty symbol is

How to pass argument in a singleton

情到浓时终转凉″ 提交于 2020-05-23 03:29:11
问题 I've been wondering how to pass argument to a singleton contructor. I already know how to do a singleton, but I've been unlucky to find a way to do it. Here is my code (part of it). Questionnary* Questionnary::getInstance(){ static Questionnary *questionnary = NULL; if(questionnary == NULL){ cout << "Object created"; questionnary = new Questionnary(); } else if(questionnary != NULL){ cout << "Object exist"; } return questionnary; } Questionnary::Questionnary(){ cout << "I am an object"; } /

Create bash select menu with array argument

北战南征 提交于 2020-02-25 07:53:29
问题 I have a function called createmenu . This function will take in an array as the first argument. The second argument will be the size of the array. I then want to create a select menu using the elements of that array. This is what I have so far: Create the menu with the given array createmenu () { echo $1 echo "Size of array: $2" select option in $1; do if [ $REPLY -eq $2 ]; then echo "Exiting..." break; elif [1 -le $REPLY ] && [$REPLY -le $2-1 ]; then echo "You selected $option which is