Is g++ 4.5.3 broken when it comes to pointers to lamba functions? - g++

I was trying out lambda functions and making a jump table to execute them, but I found g++ didn't recognize the type of the lambda function such that I could assign them to an array or tuple container.
One such attempt was this:
auto x = [](){};
decltype(x) fn = [](){};
decltype(x) jmpTable[] = { [](){}, [](){} };
On compilation I get these errors:
tst.cpp:53:27: error: conversion from ‘main()::<lambda()>’ to non-scalar type ‘main()::<lambda()>’ requested
tst.cpp:54:39: error: conversion from ‘main()::<lambda()>’ to non-scalar type ‘main()::<lambda()>’ requested
Hmmmm, can't convert from type A to non-scalar type A? What's that mean? o.O
I can use std::function to get this to work, but a problem with that is it doesn't seem to work with tuple:
function<void()> jmpTable[] = [](){}; // works
struct { int i; function<void()>> fn; } myTuple = {1, [](){}}; // works
tuple<int, function<void()>> stdTuple1 = {1, [](){}}; // fails
tuple<int, function<void()>> stdTuple2 = make_tuple(1, [](){}); // works
tst.cpp:43:58: error: converting to ‘std::tuple<int, std::function<void()> >’ from initializer list would use explicit constructor ‘std::tuple<_T1, _T2>::tuple(_U1&&, _U2&&) [with _U1 = int, _U2 = main()::<lambda()>, _T1 = int, _T2 = std::function<void()>]’
Constructor marked explicit? Why?
So my question is if I am doing something invalid or is this version just not quite up to the task?

Hmmmm, can't convert from type A to non-scalar type A? What's that mean? o.O
No, that's not a conversion to the same type. Despite having identical bodies, the different lambdas have different types. Newer versions of GCC make this clearer, and give the error message:
error: conversion from '__lambda1' to non-scalar type '__lambda0' requested
clang does even better:
error: no viable conversion from '<lambda at test.cc:2:18>' to 'decltype(x)' (aka '<lambda at test.cc:1:10>')
I can use std::function to get this to work, but a problem with that is it doesn't seem to work with tuple:
It does (with 4.5.4, at least, I don't have 4.5.3 to test), but your initialisation isn't quite right.
tuple<int, function<void()>> stdTuple1 {1, [](){}}; // lose the = to initialise stdTuple1 directly

I'm not sure about the state of n3043 in 4.5.3, but you should be able to use function pointer conversion. If I'm not misunderstanding your usage intentions, this may work for you;
void (*x)();
decltype(x) fn = [](){};
decltype(x) jmpTable[] = { [](){}, [](){} };

Related

Infer type information inside if-constexpr

I want to explore the feature of if-constexpr and try to figure out type information at compile-time.
For this purpose, I write the following code.
I expect that printTypeInfo function will return 4 for x and 3.24 for y. However, it gives me 3.1 and 3.24.
I don't know where goes wrong.
I wish decltype will infer the type to int, but it seems infer to double.
When I replace decltype with following code, it works.
enter image description here
decltype(value) in your example will always be const T&, since that's your parameter type. References aren't integral, so you always add 0.1. You could use std::remove_reference_t to get the underlying type:
auto printTypeInfo(const auto& value) {
if constexpr (std::is_integral_v<std::remove_reference_t<decltype(value)>>) {
return v + 1;
} else {
return v + 0.1;
}
}

Kotlin: "Type inference failed"

Does someone know what goes I'm doing wrong here?
// ...
val alphabet = ('a'..'z').toList()
val str = "testing"
val arr = str.split("")
for (char in arr) {
var i = alphabet.indexOf(char) // Throws Error!
// ...
}
The indexOf-method results in
"Error:(10, 26) Kotlin: Type inference failed. The value of the type parameter T should be mentioned in input types (argument types, receiver type or expected type). Try to specify it explicitly."
I have tried to "var i: Int = alphabet.indexOf(char)" and "alphabet.indexOf(char) as Int". The same result ...
What's the issue here?
I believe that your problem is that you think that the variable char contains a Char value. If that were the case, then it would make sense for indexOf to be accepting a Char value and then finding the location of that character in a list of characters (List<Char>) on which the method is being called.
But str.split() is returning a List<String>, not a List<Char>, and so char is a String. So you are asking the indexOf method to tell you the location of a String in a list of characters (List<Char>). That doesn't make sense. That's the basis of the problem. Exactly how that translates to the error you're getting from the Kotlin compiler, I'm not sure.
To make it clear, this line produces the same error:
var i = alphabet.indexOf("c") // Throws Error!
but the compiler likes this line just fine:
var i = alphabet.indexOf('c') // No Error!!!

How to fill an xtensor array, with complex data type

Paraphrasing here, but this is the problem
xt::pyarray< std::complex<double> > output;
output = 0; // fails to compile
output = double(0); // also fails to compile
output = complex<double>(0); // succeeds
and yet this is fine
std::complex<double> foo;
foo = double(0);
The problem is I'm writing a function which accepts arbitrary xtensor array, eg.
template<typename xtarray_t>
void my_function(xtarray_t &output, const xtarray_t &input) {
output = 0;
}
so in general this
output = complex<double>(0);
would fail. I've seen this, but am not sure if this was implemented, or how to use it, couldn't find the documentation. In general though, I think it should still work with std::complex.
thanks for any help!
compiler output:
xtensor-python/include/xtensor-python/pyarray.hpp:360:20: note: candidate function not viable: no known conversion from 'double' to 'const xt::pyarray<std::__1::complex<double>, xt::layout_type::dynamic>::self_type' (aka 'const pyarray<std::__1::complex<double>, (xt::layout_type)0>') for 1st argument
self_type& operator=(const self_type& rhs);
xtensor-python/include/xtensor-python/pyarray.hpp:363:20: note: candidate function not viable: no known conversion from 'double' to 'xt::pyarray<std::__1::complex<double>, xt::layout_type::dynamic>::self_type' (aka 'pyarray<std::__1::complex<double>, (xt::layout_type)0>') for 1st argument
self_type& operator=(self_type&& e) = default;
xtensor-python/include/xtensor-python/pyarray.hpp:369:20: note: candidate template ignored: could not match 'xexpression<type-parameter-0-0>' against 'double'
self_type& operator=(const xexpression<E>& e);
Answer is to do this
using value_t = typename xtarray_t::value_type;
output = value_t(0);

Need help defining a machine integer

I'm using the mach.int library in a specification (see also this question), and I'm trying to define a constant that is of type int32:
let constant_out1: int32 = 1 in
…
However, when analyzing this, why3 returns the message:
This term has type int, but is expected to have type int32
I noticed that Bounded_int (which Int32 instantiates with type int32) has the following in it:
val of_int (n:int) : t
requires { "expl:integer overflow" in_bounds n }
ensures { to_int result = n }
However, I do not seem to be able to use this to cast 1 to int32. For example, if I use:
let constant_out1: int32 = Int32.of_int(1) in
…
I get the message unbound symbol 'Int32.of_int'. I've tried many permutations of this, all without any success. Can anyone provided guidance for how to tell why3 that I want 1 to be of type int32?

Chaining iterators of different types

I get type errors when chaining different types of Iterator.
let s = Some(10);
let v = (1..5).chain(s.iter())
.collect::<Vec<_>>();
Output:
<anon>:23:20: 23:35 error: type mismatch resolving `<core::option::Iter<'_, _> as core::iter::IntoIterator>::Item == _`:
expected &-ptr,
found integral variable [E0271]
<anon>:23 let v = (1..5).chain(s.iter())
^~~~~~~~~~~~~~~
<anon>:23:20: 23:35 help: see the detailed explanation for E0271
<anon>:24:14: 24:33 error: no method named `collect` found for type `core::iter::Chain<core::ops::Range<_>, core::option::Iter<'_, _>>` in the current scope
<anon>:24 .collect::<Vec<_>>();
^~~~~~~~~~~~~~~~~~~
<anon>:24:14: 24:33 note: the method `collect` exists but the following trait bounds were not satisfied: `core::iter::Chain<core::ops::Range<_>, core::option::Iter<'_, _>> : core::iter::Iterator`
error: aborting due to 2 previous errors
But it works fine when zipping:
let s = Some(10);
let v = (1..5).zip(s.iter())
.collect::<Vec<_>>();
Output:
[(1, 10)]
Why is Rust able to infer the correct types for zip but not for chain and how can I fix it? n.b. I want to be able to do this for any iterator, so I don't want a solution that just works for Range and Option.
First, note that the iterators yield different types. I've added an explicit u8 to the numbers to make the types more obvious:
fn main() {
let s = Some(10u8);
let r = (1..5u8);
let () = s.iter().next(); // Option<&u8>
let () = r.next(); // Option<u8>
}
When you chain two iterators, both iterators must yield the same type. This makes sense as the iterator cannot "switch" what type it outputs when it gets to the end of one and begins on the second:
fn chain<U>(self, other: U) -> Chain<Self, U::IntoIter>
where U: IntoIterator<Item=Self::Item>
// ^~~~~~~~~~~~~~~ This means the types must match
So why does zip work? Because it doesn't have that restriction:
fn zip<U>(self, other: U) -> Zip<Self, U::IntoIter>
where U: IntoIterator
// ^~~~ Nothing here!
This is because zip returns a tuple with one value from each iterator; a new type, distinct from either source iterator's type. One iterator could be an integral type and the other could return your own custom type for all zip cares.
Why is Rust able to infer the correct types for zip but not for chain
There is no type inference happening here; that's a different thing. This is just plain-old type mismatching.
and how can I fix it?
In this case, your inner iterator yields a reference to an integer, a Clone-able type, so you can use cloned to make a new iterator that clones each value and then both iterators would have the same type:
fn main() {
let s = Some(10);
let v: Vec<_> = (1..5).chain(s.iter().cloned()).collect();
}
If you are done with the option, you can also use a consuming iterator with into_iter:
fn main() {
let s = Some(10);
let v: Vec<_> = (1..5).chain(s.into_iter()).collect();
}