Creating a dup function inside a module - clashes with dup property of array - d

If I create a dup function inside my D program, I can no longer use the dup property of an array. This code works
import std.stdio;
void main() {
double[] v = [0.1, 0.2, 0.3];
writeln(v.dup);
}
But this code returns "clash.d(9): Error: function clash.dup (double x) is not callable using argument types (double[])"
import std.stdio;
double dup(double x) {
return x;
}
void main() {
double[] v = [0.1, 0.2, 0.3];
writeln(v.dup);
}
How can I define a dup function in my program and not lose the dup property of arrays?

alias it into the local overload set:
alias dup = object.dup;
right below your own dup function definition.

Related

How to program a function whose return value is depending on the flag without using "if"?

I want to make a class function like the conceptual code below.
double function(){
if(flag==true){
"some process and return "
}
else{
"another process and return"
}
}
where flag is the boolean member of the class.
I want to make this function without using if because I use this function many times.
The points are
I want to use the same function with the two cases of the process.
I want to avoid re-evaluation of a flag that doesn't change its value for some period.
If you want to call one of two different member functions depending on the value of a bool without using an if or something else that might lead to branching, you could create a table containing two function pointers and use that for lookup by using the bool for indexing. If you only want to do this lookup when the value of the flag changes, you could store a pointer to the active function and only do the lookup when flag is set.
Example where the member functions are also taking an argument:
#include <iostream>
class foo {
public:
using func_t = double(foo::*)(double); // the type of the member functions
double some(double x) { return x * 3.14159; }
double another(double x) { return x * 3.14159 * 3.14159; }
double function(double x) {
return (this->*active)(x); // "active" only changes when "flag" is set
}
void set(bool x) {
flag = x;
// lookup without "if" to set the active function:
active = funcs[flag];
}
private:
// a static table of the functions to be called - only initialized once
static constexpr func_t funcs[]{&foo::some, &foo::another};
bool flag = false;
func_t active = &foo::some; // the active function
};
int main() {
foo x;
x.set(false);
std::cout << x.function(2.) << '\n';
x.set(true);
std::cout << x.function(3.) << '\n';
}
Output
6.28318
29.6088
Class function, flag, and two different behaviors? You probably should make two derived classes, drop the flag, and use a virtual function instead.
You can just cast the boolean into an int
int test(bool flag)
{
return static_cast<int>(flag);
}
int not_test(bool flag)
{
return static_cast<int>(!flag);
}
Remark: By the time this answer was posted, the question was completely different.

Function with empty imput [duplicate]

I want to set default value in argument of function to Rcpp::Function argument.
Just simple assignment, Rcpp::Function func = mean, is not possible. It returns error: no viable conversion from '<overloaded function type>' to 'Rcpp::Function' (aka 'Function_Impl<PreserveStorage>')
Or, I tried something like this: Rcpp::Function func = Function("mean"), but again, it is not working. It returns warning message: Unable to parse C++ default value 'Function("mean")' for argument func of function.
For example, I have my own function to maximum called maxC:
// [[Rcpp::export]]
double maxC(NumericVector x) {
double max;
max = *std::max_element(x.begin(), x.end());
return max;
}
Now, I want to use it (maxC) as default argument to another function, for example like this:
// [[Rcpp::export]]
double aggregate(NumericVector x, Rcpp::Function func = maxC) {
double agg;
agg = Rcpp::as<double>(func(x));
return agg;
}
But it doesn't work. Any suggestions? Thank you.
I do not believe you can set a default function in this way... The best that can be achieved is setting function to a NULL value and then having the code execute the appropriate default later on. For example...
#include <Rcpp.h>
// [[Rcpp::export]]
Rcpp::NumericVector func_defaults(Rcpp::NumericVector x,
Rcpp::Nullable<Rcpp::Function> f = R_NilValue) {
if (f.isNotNull()) {
Rcpp::NumericVector res = Rcpp::as<Rcpp::Function>(f)(x);
return res;
}
Rcpp::Environment global_funcs = Rcpp::Environment::global_env();
Rcpp::Function mean_r = global_funcs["mean"];
return mean_r(x);
}
testing:
func_defaults(c(2.5,3,1))
# [1] 2.166667
func_defaults(c(2.5,3,1), mean)
# [1] 2.166667
func_defaults(c(2.5,3,1), median)
# [1] 2.5

after setting a member variable value the value is changing inside getter function

I am trying to set two member variable which are float type and also succeed for one(which is nothing but directly assigned inside of a setter function). But another one need some calculation and with static_cast<float> I have configured it after calculation. For the second variable I have checked it inside of the setter function and found it is configured correctly but while I have tried to fetch it through getter function or directly by class object it is showing some weird value.
source code:
# include <iostream>
# include <math.h>
using namespace std;
class dosomething{
public:
float res_value_a;
float res_value_b;
void set_res_value_a ()
{
res_value_a = 0.35;
}
float get_res_value_a(){
return res_value_a;
}
void set_res_value_b(int set_value_b)
{
int b = pow (2, 6);
float res_value_b = static_cast<float>(set_value_b)/b;
cout<<"value of res_value_b after setting is: "<<res_value_b<<endl;
}
float get_res_value_b()
{
cout<<"value of res_value_b inside of getter function: "<<res_value_b<<endl;
return res_value_b;
}
};
int main()
{
dosomething obj;
obj.set_res_value_a();
float ret_get_val_a = obj.get_res_value_a();
cout<<"ret_get_val_a: "<<ret_get_val_a<<endl; // return value from function get_res_value_a
cout<<"member variable res_value_a: "<<obj.res_value_a<<endl; // checking member variable value by object
int set_value_b;
cin>> set_value_b;
obj.set_res_value_b(set_value_b);
float ret_get_val_b = obj.get_res_value_b();
cout<<"ret_get_val_b: "<<ret_get_val_b<<endl; // return value from function get_res_value_b
cout<<"member variable res_value_b: "<<obj.res_value_b<<endl; // checking member variable value by object
return 0;
}
For set_res_value_b function I am also giving required input and expected output--
if set_value_b = 64 then res_value_b = 1
if set_value_b = 20 then res_value_b = 0.3125
if set_value_b = 134 then res_value_b = 2.09375
One error value I can give which got for set_value_b = 20 and that is res_value_b = 4.59149e-41. What could be the cause for this error??
The line
float res_value_b = static_cast<float>(set_value_b)/b;
is setting the value of not the member variable but a local variable.
Remove the first float to eliminate the local variable and have it set the member variable.
res_value_b = static_cast<float>(set_value_b)/b;

How do I return a reference to a dynamic type from C++ and hold it in Python?

I'm writing some code that returns a column value from a row based on the column index. The row is updated from time to time in C++, and I'd like the python code to keep a reference to the column value. The following code reflects my current solution, in which I have to repeatedly fetch the column value.
struct Foo
{
PyObject * get(int pos)
{
// Position 0 is an integer value. Position 1 is a float value
if (pos == 0)
return Py_BuildValue ("i", m_int);
else
return Py_BuildValue ("f", m_float);
}
void set_int(int i)
{
m_int = i;
}
void set_float(float f)
{
m_float = f;
}
int m_int;
float m_float;
};
My bindings are simple:
class_<Foo> ("Foo")
.def("get", &Foo::get)
.def("set_int", &Foo::set_int)
.def("set_float", &Foo::set_float)
;
This works at the python level like this:
In [16]: foo = Foo()
In [17]: foo.set_int(1)
In [18]: foo.set_float(2.5)
In [19]: i = foo.get(0)
In [20]: f = foo.get(1)
In [21]: i
Out[21]: 1
In [22]: type(i)
Out[22]: int
In [23]: f
Out[23]: 2.5
In [24]: type(f)
Out[24]: float
So far, so good. However, when I modify foo, I'd like i and f to reflect the new values. Currently, they reflect the old values.
In [25]: foo.set_int(42)
In [26]: i
Out[26]: 1
How do I setup the C++ and binding code so that 'get' returns a reference to rather than a copy of the Foo member variables?
You need to wrap the integer values in an object class. Primitives are immutable in Python so when you return an int value it will never reference the original int, just a const copy. You could return a new struct of the form:
struct IntWrap {
int *value;
int get() { return *value; }
void set_int(int nval) { *value = nval; }
...
};
class_<IntWrap > ("IntWrap")
.def("get", &IntWrap::get)
.set("set_int", &IntWrap::set_int)
...
and make wrapper function bindings that treat the struct as a mutable integer in Python. This however, does violate the constness idiom of primitives in Python.
Here's a post on effectively inheriting from str or int, where you could make changes to the integer style object to use IntWrap instead (in case you want something that acts more like an int out of the box).

Passing a 2D array of Structs by reference in C++

SO im pretty new to c++ and im trying to pass a 2D array of a struct type by reference to a function. As far as i know they are automatically passed by reference. Here is my code.The problem is probably obvious but i cant figure it out. The complier keeps saying variable or field "function" declared void and bArray was not declared in this scope.
void function(balloons bArray[][5]);
int main()
{
struct balloons
{
float totalWeight;
float largestBalloon;
};
balloons balloonsArray[20][5];
function(balloonsArray);
}
void function(balloons bArray[][5])
{
bArray[1][1].totalWeight = 1.0
bArray[1][1].largestBalloon = 1.0
}
You're defining your struct within main, other parts of your code need to use it also. Move the definition outside the function:
struct balloons
{
float totalWeight;
float largestBalloon;
};
void function(balloons bArray[][5]);
int main()
{
// ...
And you haven't terminated the two statements in your function, you'll need semicolons there:
bArray[1][1].totalWeight = 1.0;
bArray[1][1].largestBalloon = 1.0;