Taking Eigen::Vector as a parameter - c++

I am trying to write a function that takes an Eigen::Vector<T, dim> as a parameter. However, the following example fails to compile:
#include <Eigen/Core>
template<class F, typename T, int dim>
void bar(F&& func, const Eigen::Vector<T, dim>& arg1) {
}
template<typename T, int dim>
void foo(const Eigen::Vector<T, dim>& a) {
return bar([] {}, a);
}
int main() {
Eigen::Vector<float, 3> v1{ 1.f,2.f,3.f };
foo(v1);
return 0;
}
This, under Visual Studio 2019, gives me the following error:
1>main.cpp(9,10): error C2672: 'bar': no matching overloaded function found
1>main.cpp(14): message : see reference to function template instantiation 'void foo<float,3>(const Eigen::Matrix<float,3,1,0,3,1> &)' being compiled
1>main.cpp(9,1): error C2784: 'void bar(F &&,const Eigen::Matrix<T,dim,1,|_Rows==&&?:&&_Rows!=?:,_Rows,1> &)': could not deduce template argument for 'const Eigen::Matrix<T,dim,1,|_Rows==&&?:&&_Rows!=?:,_Rows,1> &' from 'const Eigen::Matrix<float,3,1,0,3,1>'
1>main.cpp(4): message : see declaration of 'bar'
My questions:
What is this weird |_Rows==&&?:&&_Rows!=?: in the error message?
What can I do to make the above code compile?
The bar function should have T and dim availabe. I cannot just take const AnyType& arg1, because the actual implementation of bar depends on compile-time known values T and dim.
I have seen https://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html. I think I understand what they are saying, but I am not sure if it applies here. I am taking an actual Eigen::Vector as an argument, not an expression.
If there was an expression it would be fine for me, to have it materialized.
Nevertheless, if I try to follow their instruction and just use ArrayBase<Derived>, I lose the compile-time information about T and dim.

This indeed looks like an MSVC issue, it compiles fine with gcc >= 4.7, and clang >= 3.5: https://godbolt.org/z/kqoHyO
One possible workaround would be to explicitly write out what Eigen::Vector expands to:
template<class F, typename T, int dim>
void bar(F&& func, const Eigen::Matrix<T, dim, 1, 0, dim, 1>& arg1) {
}
https://godbolt.org/z/vlvSDP
The weird |_Rows==&&?:&&_Rows!=?: looks like MSVC mangled the default value of the Options template parameter:
AutoAlign |
( (_Rows==1 && _Cols!=1) ? Eigen::RowMajor
: (_Cols==1 && _Rows!=1) ? Eigen::ColMajor
: EIGEN_DEFAULT_MATRIX_STORAGE_ORDER_OPTION ),
If you want to get to the bottom of this, you should file a bug-report to the MSVC maintainers, maybe using a simplified example like this: https://godbolt.org/z/U_0Sh7 (probably it's possible to reduce this even more).

Related

Avoiding ambiguous parameters in templated Eigen functions with MSVS

I'm writing some funtions that are supposed to take Eigen::Array as an input.
The Arrays are of constant size, but the size is a template parameter and should be deducted from the input.
When compiling with MSVS, i have to supply the size to the function or it will result in an error.
#include <Eigen/Core>
template<unsigned short t_iSize>
void foo(const Eigen::Array<unsigned short, t_iSize, 1>&)
{
}
int main()
{
Eigen::Array<unsigned short, 3, 1> test;
// foo(test); // Compiler errors C2672 and C2784
foo<3>(test); // Giving the size solves the errors
}
The size should be possible to be deducted from the variable test, but it seems to fail when computing the template arguments 4 and 5 for the Array.
Error C2672: "foo": no matching overloaded function found.
Error C2784: "void foo(const Eigen::Array< unsigned short,t_iSize,1,|_Rows==&&?:&&_Rows!=?:,_Rows,1> &)": could not deduce template argument for "const Eigen::Array< unsigned short,t_iSize,1,|_Rows==&&?:&&_Rows!=?:,_Rows,1> &" from "Eigen::Array< unsigned short,3,1,0,3,1>".
Is it possible to avoid this problem when taking the Eigen::Array, or do I need to take Eigen::ArrayBase as a function parameter? I would like to avoid that, as it obscures the fact that the function only takes this specific type of arrays.
Edit:
As Jarod42 noted, the template parameter should be of type int.
Visual Studio can compile the code with this mistake.
It fails in deducing the parameter _Rows, though, where other compilers can do so.
Here you can see the problem I ran into.
Declaration of Eigen::Array is
template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
class Eigen::Array;
Your function uses wrong type for row, unsigned short should be int.
template <int t_iSize>
void foo(const Eigen::Array<unsigned short, t_iSize, 1>&)
{
// ...
}
Demo
As workaround for your issue with Msvc, you might do:
apply the default value manually:
template <int t_iSize>
void bar(const Eigen::Array<unsigned short, t_iSize, 1, 0, t_iSize, 1>&) {
// ...
}
or add the extra template (and so the code is even more generic):
template <int t_iSize, int Options, int MaxRows, int MaxCols>
void foo(const Eigen::Array<unsigned short, t_iSize, 1, Options, MaxRows, MaxCols>&) {
// ...
}
Demo

Wrong pack expanded in variadic template

I got a very strange problem with variadic templates. It seems the wrong pack is expanded. Here's a code snippet:
#include <tuple>
template<typename...>
struct types {};
template<typename = types<>>
struct Base;
template<typename... Args1>
struct Base<types<Args1...>> {
template<typename... Args2>
static auto construct(Args1... args1, Args2&&... args2)
-> decltype(std::make_tuple(args1.forward()..., std::declval<Args2>()...))
{
return std::make_tuple(args1.forward()..., std::forward<Args2>(args2)...);
}
};
struct Derived : Base<> {};
int main() {
auto test = &Derived::construct<char const(&)[7]>;
}
I get this error:
13 : <source>:13:43: error: request for member 'forward' in 'args2#0', which is of non-class type 'const char [7]'
-> decltype(std::make_tuple(args1.forward()..., std::declval<Args2>()...))
~~~~~~^~~~~~~
13 : <source>:13:43: error: request for member 'forward' in 'args2#0', which is of non-class type 'const char [7]'
<source>: In function 'int main()':
22 : <source>:22:27: error: unable to deduce 'auto' from '& construct<const char (&)[7]>'
auto test = &Derived::construct<char const(&)[7]>;
^~~~~~~~~~~~~~~~~~~~~~~~~~~
22 : <source>:22:27: note: could not resolve address from overloaded function '& construct<const char (&)[7]>'
Compiler exited with result code 1
However, it don't happen when the pack has values in it:
struct HasForward { int forward() { return 0; } };
struct Derived : Base<types<HasForward>> {};
Here's the First snippet live and the Second snippet live
What's wrong with this code? Is this a compiler bug? Is there any ways to overcome it and leave the first pack empty?
Is this a compiler bug? Is there any ways to overcome it and leave the first pack empty?
It looks like a bug in your compiler.
To work around it you can use a function declaration (no definition required) like the one in the following example and use it to test your parameters:
template<typename... Args1>
class Base<types<Args1...>> {
template<typename... T, typename... U>
static auto ret(types<T...>, types<U...>)
-> decltype(std::make_tuple(std::declval<T>().forward()..., std::declval<U>()...));
public:
template<typename... Args2>
static auto construct(Args1... args1, Args2&&... args2)
-> decltype(ret(types<Args1...>{}, types<Args2...>{}))
{
return std::make_tuple(args1.forward()..., std::forward<Args2>(args2)...);
}
};
A bit ugly, but it works when your first pack is empty (also in C++11 as requested) and everything should be discarded by the linker.
--- EDIT
As suggested by #W.F. in the comments (thanks for the suggestion, I didn't notice it), it's even easier to accomplish that.
Just define your function as it follows:
static auto construct(Args1... args1, Args2&&... args2)
-> decltype(std::make_tuple(std::declval<Args1>().forward()..., std::declval<Args2>()...))
{
return std::make_tuple(args1.forward()..., std::forward<Args2>(args2)...);
}

Why couldn't deduce const& template parameter?

Consider this code:
constexpr int TEN = 10;
template < const int& >
struct Object { };
template < const int& II >
void test(Object<II>) { }
Then the calls:
test<TEN>(Object<TEN>{}); // passes
test(Object<TEN>{}); // FAILS
The second call fails to compile with error message:
error: no matching function for call to ‘test(Object<TEN>)
note: candidate: template<const int& II> void test(Object<II>)
note: template argument deduction/substitution failed:
note: couldn't deduce template parameter ‘II’
The question is why? Is it according to the standard?
And the more important question is: how can I workaround this? That is: how can I help the compiler to deduce the const int& template parameter?
In the real code instead of int I have more complex literal type, so I do need the const&. Thus I can't just "use int instead of const int&".
I am using gcc-7.0.1 (the snapshot) and I am getting the same error with options -std=c++11, -std=c++14, -std=c++17.

Is templated alias conducting inner and outer parameter packs non-deduced context?

The problem came from here - I wanted to create an approach solving a little bit more general problem. Consider an example:
#include <utility>
template<class T, std::size_t>
using deduced = T;
template<std::size_t N, class = std::make_index_sequence<N>>
struct Foo;
template<std::size_t N, std::size_t... Is>
struct Foo<N, std::index_sequence<Is...>>{
template <class... Args>
void Bar(deduced<Args, Is>...)
{ }
};
int main() {
Foo<3> myfoo;
myfoo.Bar(true, 2.0f, 3); // OK
//myfoo.Bar(1, 2, 3, 4); // error
}
clang has no problem with compiling the code, gcc on the other hand shows following errors:
prog.cc: In function 'int main()':
prog.cc:18:27: error: no matching function for call to 'Foo<3ul>::Bar(bool, float, int)'
myfoo.Bar(true, 2.0f, 3); // valid
^
prog.cc:12:10: note: candidate: template<class ... Args> void Foo<N, std::integer_sequence<long unsigned int, Is ...> >::Bar(deduced<Args, Is>...) [with Args = {Args ...}; long unsigned int N = 3ul; long unsigned int ...Is = {0ul, 1ul, 2ul}]
void Bar(deduced<Args, Is>...)
^~~
prog.cc:12:10: note: template argument deduction/substitution failed:
prog.cc:18: confused by earlier errors, bailing out
[live demo]
What confuses me gcc does not have a problem with deduction when using the same alias but outer parameter pack isn't involved, e.g.:
void Bar(deduced<Args, 0>...)
So the question is - is it legal to combine parameter packs from outer and inner class with alias of this form to make compiler deduce one of the template parameters or is it gcc bug?
Edit (based on bogdan's comment):
The code causes trouble also to MSVC (2017 RC), but works in this form with EDG compiler, icc (in version 16 and 17) also seem to deal well with a code. It is also worth noting that similar code with class instead of alias (also considered as deduced context in some places example by bogdan) causes even more trouble to compilers - only clang seems to deal well with this version(?)

Detecting parameter types from generic lambda - compile error with GCC

I wrote some code that retrieves the types of the non-auto parameters when given a generic lambda function. As you can see in the code below, the idea is to call the connect function with a generic lambda and provide arguments for the auto parameters (which will always be at the front in my use case). So in the code below my goal was to detect that the second parameter is of type float.
The code works fine with clang 3.8 but it doesn't compile with gcc 6.1.1, so I was wondering whether this was a bug in gcc or if this is just not valid c++ code? Can I assume that a generic lambda is implemented with a templated operator() function or is this compiler-specific?
template <typename Functor, typename... AllArgs, typename... ProvidedArgs>
void findArgTypes(void(Functor::*)(AllArgs...) const, Functor, ProvidedArgs...)
{
// AllArgs == int, float
// ProvidedArgs == int
}
template <typename Func, typename... ProvidedArgs>
void connect(Func func, ProvidedArgs... providedArgs)
{
findArgTypes(&Func::template operator()<ProvidedArgs...>, func, providedArgs...);
}
int main()
{
int tmp = 0;
connect([&](auto, float){ ++tmp; }, 0);
}
The error that gcc gives is this:
main.cpp: In instantiation of ‘void connect(Func, ProvidedArgs ...) [with Func = main()::<lambda(auto:1, float)>; ProvidedArgs = {int}]’:
main.cpp:16:33: required from here
main.cpp:11:17: error: no matches converting function ‘operator()’ to type ‘void (struct main()::<lambda(auto:1, float)>::*)() const’
findArgTypes(&Func::template operator()<ProvidedArgs...>, func, providedArgs...);
~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:16:27: note: candidate is: template<class auto:1> main()::<lambda(auto:1, float)>
connect([](auto, float){}, 0);
^
Removing the const in findArgTypes gives the same result.
Using the following code works with both compilers:
struct Foo
{
template <typename T>
void operator()(T, float) const {}
};
int main()
{
Foo f;
connect(f, 0);
}
You have error because you are expecting functor (object) but lambda with empty capture is convertible to free function:
int main() {
using function = void (*)(int, float);
function a = [](auto, float){};
}
See lambda from cppreference:
For the newest version of your question that implementation satisfies both compilers:
template <typename Func, typename... ProvidedArgs>
void connect(Func func, ProvidedArgs... providedArgs)
{
auto mf = &Func::template operator()<ProvidedArgs...>;
findArgTypes(mf, func, providedArgs...);
}
I think this is gcc compiler bug that gcc needs this auto local variable to work correctly...
BTW, one question - one bug in clang, one in gcc - I really advice you to find simpler way to achieve your goals - maybe consider to just use std::function instead of quite fresh generic-lambda?