Initializing C arrays with __auto_type - gcc

I tried to initialize an array using __auto_type in GNU C, but this seems to be a syntax error:
#include <stdio.h>
int main() {
__auto_type a[] = {1,2,3,4};
printf("%d",a[0]);
return 0;
}
As a workaround, I can define a variadic macro to initialize the array using typeof, but this is more cumbersome:
#include <stdio.h>
#define infer_array_type(a,b,...) typeof(b) a[] = {b,__VA_ARGS__}
int main() {
infer_array_type(a,1,2,3,4);
printf("%d",a[0]);
return 0;
}
Is it not possible to initialize arrays in GNU C using __auto_type instead of typeof?

In GNU C, it is possible to define a statement expression that returns an array, so the array can be initialized using __auto_type:
#include <stdio.h>
#define infer_array_type(b,...) ({typeof(b) a[] = {b,__VA_ARGS__}; a;})
int main() {
__auto_type a = infer_array_type(1,2,3,4);
printf("%d",a[0]);
return 0;
}

Related

where did i make the problem in declaring the function average in this code c++

#include <iostream>
using namespace std;
double Average(inta, intb, intc);
int main()
{
int num1, num2,num3,z;
cout<<"enter three numbers";
cin>>num1>>num2>>num3;
z=Average(num1,num2,num3);
cout<<"Average="<<z;
return 0;
}
You have not specified types for the 3 input parameters and you didn't write a function body.

Why is the output different? (putting array in a function problem)

I want the output to be: 1 2 2 2
But why is the output: 1 2 3 4
What's wrong with this code?
#include <iostream>
using namespace std;
int arr[] = {0};
int pluss(int ar[],int a){
ar[0]++;
cout<<ar[0]<<endl;
if(a==0){
pluss(ar,a+1);
pluss(ar,a+1);
pluss(ar,a+1);
}
}
int main() {
pluss(arr,0);
return 0;
}
EDIT: So, the "ar" is global and not local to one child function? how to make it so the "ar" is only local to one child function? I mean: the "ar" in the first pluss(ar,1) is different from the "ar" in the second pluss(ar,2)?
Your code is equivalent of :
int main() {
pluss(arr,0);
pluss(arr,1);
pluss(arr,1);
pluss(arr,1);
return 0;
}
Since each call to pluss definitely increments the array element, before printing it, expected output is 1, 2, 3, 4.
how to make it so the "ar" is only local to one child function?
If you don't like to pass each array element as integer value, you could wrap the array in a struct, since structures are passed by value rather than by reference.
#include <iostream>
using namespace std;
struct s { int a[1]; } arr = {0};
int pluss(struct s ar, int a)
{
ar.a[0]++;
cout <<ar.a[0] <<endl;
if (a==0)
{
pluss(ar, a+1);
pluss(ar, a+1);
pluss(ar, a+1);
}
}
int main()
{
pluss(arr, 0);
return 0;
}

Eigen boolean matrix plus

I would like to do the boolean matrix plus. How could I do it in Eigen?
My following example only gives a scalar +.
#include "Eigen/Dense"
#include <iostream>
using namespace std;
using namespace Eigen;
int main()
{
Eigen::Matrix<bool, 4, 4> m;
m << 0,1,1,1,
1,0,1,0,
1,1,0,0,
1,1,1,0;
cout << m + m; //should be logical_and here
}
How could I use the logical_and here?
Eigen does not seem to provide specific functions to work on boolean matrices. However you can use the fact that booleans are converted to 0 (false) and 1 (true) reliably (see bool to int conversion). Noting that 0=0*0=0*1=1*0 and 1*1=1 it is obvious that multiplication of the booleans as integers is the same (up to type) as logical and. Therefore the following should work:
#include "Eigen/Dense"
#include <iostream>
using namespace std;
using namespace Eigen;
int main()
{
Eigen::Matrix<bool, 4, 4> m;
m << 0,1,1,1,
1,0,1,0,
1,1,0,0,
1,1,1,0;
Eigen::Matrix<bool, 4, 4> result = m.cwiseProduct(m);
cout << result;
}

Is it possible to put std::list::iterator into std::set?

Is it possible to put the iterator of list in to set:
I wrote codes as follows :
It failed on VS2015 but run smoothly on g++
And I also tried to use std::hash to calculate a hash value of std::list::iterator
but failed again, it has no hash func for iterator.
And one can help ? Or it's impossible .....
#include <set>
#include <list>
#include <cstring>
#include <cassert>
// like std::less
struct myless
{
typedef std::list<int>::iterator first_argument_type;
typedef std::list<int>::iterator second_argument_type;
typedef bool result_type;
bool operator()(const std::list<int>::iterator& x,const std::list<int>::iterator& y) const
{
return memcmp(&x, &y, sizeof(std::list<int>::iterator)) < 0; // using memcmp
}
};
int main()
{
std::list<int> lst = {1,2,3,4,5};
std::set<std::list<int>::iterator,myless> test;
auto it = lst.begin();
test.insert(it++);
test.insert(it++);
assert(test.find(lst.begin()) != test.end()); // fail on vs 2015
auto it1 = lst.end();
auto it2 = lst.end();
assert(memcmp(&it1,&it2,sizeof(it1)) == 0); // fail on vs 2015
system("pause");
return 0;
}
Yes, you can put std::list<T>::iterator in a std::set, if you tell std::set what order they should be in. A reasonable order could be std::less<T>, i.e. you sort the iterators by the values they point to (obviously you then can't insert an std::list::end iterator). Any other order is also OK.
However, you tried to use memcmp, and that is wrong. The predicate used by set requires that equal values compare equal, and there is no guarantee that equal iterators (as defined by list::iterator::operator==) also compare equal using memcmp.
I find a way to do this as like but not for the end iterator
bool operator<(const T& x, const T& y)
{
return &*x < &*y;
}

Issue in passing argument to std::function for vector of functions

I'm trying to create a vector of std::function and then pass that vector to a function. I also need to pass arguments to the function objects, so I'm using std::bind. Here is the code:
#include <functional>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void execute(vector<function<void (int)>>& fs) {
for (auto& f : fs)
f();
}
void func(int k) {
cout << "In func " << k << endl;
}
int main()
{
int i = 1;
vector<function<void (int)>> x;
auto f1 = bind(func, i);
//f1(); // this does call intended function
x.push_back(f1);
execute(x);
}
but this gives following error:
function_tmpl.cpp: In function ‘void execute(std::vector >&)’:
function_tmpl.cpp:14:5: error: no match for call to ‘(std::function) ()’
f();
^
In file included from function_tmpl.cpp:1:0:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/functional:2142:11: note: candidate is:
class function
^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/functional:2434:5: note: _Res std::function::operator()(_ArgTypes ...) const [with _Res = void; _ArgTypes = {int}]
function::
^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/functional:2434:5: note: candidate expects 1 argument, 0 provided
If I call f() inside main(), that works fine, which means that the function has bound with the arguments, but it's not working when passed to another function as argument
You are using a vector of void functions with a single int argument: vector<function<void (int)>>, but you are actually pushing void(void) functions. All you need to do is to change the element type of the vector to vector<function<void (void)>>. Bind works roughly like this:
given:
void f1(int i) { printf("%d", i); }
bind(f1, 1) returns a new function f2:
void f2()
{
f1(1);
}
and since you are pushing f2, the vector should store void(void) functions.
After binding, the type of function has become to void(). So change the type of vector to vector<function<void ()>>, you'll get it.
#include <functional>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void execute(vector<function<void ()>>& fs) {
for (auto& f : fs)
f();
}
void func(int k) {
cout << "In func " << k << endl;
}
int main()
{
int i = 1;
vector<function<void ()>> x;
auto f1 = bind(func, i);
x.push_back(f1);
execute(x);
}
result:
In func 1
LIVE
The return type of std::bind is unspecified. Hence you cannot expect std::bind to return a variable of same type as std::function<void(int)>. Use decltype and templates to resolve.
Here is an example
#include <functional>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
template <typename T>
void execute(vector<T>& fs) {
for (auto& f : fs)
f();
}
void func(int k) {
cout << "In func " << k << endl;
}
int main()
{
int i = 1;
auto f1 = bind(func, i);
vector<decltype(f1)> x; //deduce type of f1
x.push_back(f1);
execute(x);
}
f is of type
function<void (int)>&
so the compiler expects you to provide a parameter, like this:
f(1)

Resources