Eigen Library: Compiler error when passing Block reference to templated function - eigen

I am getting a compilation error when trying to call templated functions with arguments that are Eigen references.
For example, consider the following simplified example:
template <typename derived>
inline void fcn(Eigen::MatrixBase<derived> &M){
// ...
M.template block<3,3>(0,0) = something here
// ...
}
// I get compiler errors when trying to call this function as follows:
Eigen::MatrixXd A(100,100);
// call the above function with a Block as an argument
fcn(A.block<9,6>(10,10));
The compiler complains that I am trying to instantiate a non-const reference with an object passed by value (if my undersdanding of the following is correct):
error: invalid initialization of non-const reference of type ‘Eigen::MatrixBase<Eigen::Block<Eigen::Matrix<double, -1, -1>, 9, 6, false> >&’ from an rvalue of type ‘Eigen::MatrixBase<Eigen::Block<Eigen::Matrix<double, -1, -1>, 9, 6, false> >’
If I try to declare the argument of fcn as const then I am getting an error when I try to modify it inside the function.
The only solution I've found to work to fix this issue is to declare the arument of the function to be a const &, and then to use const_cast to "remove the const qualifier" when accessing M inside the function.
But this is hacky and I makes the code extremely messy. Am I missing some obvious solution to this problem? Any help would be appreciated.
Thanks!
T

This is because in c++ you cannot bind a temporary object (here the proxy object returned by .block()) to a non-const reference. The solution is to name it:
auto A_block = A.block<9,6>(10,10);
fcn(A_block);

Related

libclang: how to get non-type template arguments from a CXType?

Given the following code to parse:
template <typename T, int N=3>
class Base {};
using BaseFloat1 = Base<float, 1>;
I can get the TypeAliasDecl from the last line, get the CXType from that, and the use clang_Type_getNumTemplateArguments() to see that there are two template arguments there, but the only option for retrieving the arguments is clang_Type_getTemplateArgumentAsType(), which obviously fails for the second, IntegerLiteral argument.
Is there any way to get the non-type template argument directly, other than inspecting the children of the TypeAliasDecl cursor?

Passing shared_ptr as an argument to thread function, results into an error

I am trying to do something similar as below:
std::shared_ptr<asengine::PreCompileConfig> precompileconfigobj = std::make_shared<asengine::PreCompileConfig>();
std::thread rsync_t(&asengine::PreCompile::RunRsyncDb, precompileconfigobj));
I get the following error:
/usr/include/c++/4.8/functional:1697:61: error: no type named 'type' in 'class std::result_of<std::_Mem_fn<void (asengine::PreCompile::*)(const std::shared_ptr<asengine::PreCompileConfig>&)>(std::reference_wrapper<std::shared_ptr<asengine::PreCompileConfig> >)>'
typedef typename result_of<_Callable(_Args...)>::type result_type;
^
/usr/include/c++/4.8/functional:1727:9: error: no type named 'type' in 'class std::result_of<std::_Mem_fn<void (asengine::PreCompile::*)(const std::shared_ptr<asengine::PreCompileConfig>&)>(std::reference_wrapper<std::shared_ptr<asengine::PreCompileConfig> >)>'
_M_invoke(_Index_tuple<_Indices...>)
After brief research, I could find out that thread function expects reference of the argument object, instead of value.
std::thread rsync_t(&asengine::PreCompile::RunRsyncDb, std::ref(precompileconfigobj));
But still, it doesn't solve the problem. Any help to figure out the problem would be appreciated.
I think I solved the problem. I had to pass the object through which thread function is going to be called.
std::thread rsync_t(&asengine::PreCompile::RunRsyncDb, precompileobj, std::ref(precompileconfigobj));

Variadic template argument deduction - c++11 - many functions linked to one

I'm experiencing some strange occurrences regarding varidaic templates that I never saw before.
In order to keep it simple i'll give a simple example of what I was trying to achieve, the real code is a bit more involved.
So I have one function that looks like:
template <typename T, typename F, typename ... Args>
static T Func( std::string str, Args&& ... args )
{
... Do something
}
I then call this function a number of times from different locations, where most times the passed types are different. But when I debug this, when I look for the symbol of Func, I get that the same function address has a number of different symbols:
00000000`70ba4ae0 Func<unsigned long,unsigned long (__stdcall*)(wchar_t const *),wchar_t const *>
00000000`70ba4ae0 Func<void *,void * (__stdcall*)(unsigned int),unsigned int>
00000000`70ba4ae0 Func<int,int (__stdcall*)(void *),void *>
So they are all basically the same function. When I try to call for example:
call Func<void *,void * (__stdcall*)(unsigned int),unsigned int>
In the debugger I see:
call Func<int,int (__stdcall*)(void *),void *>
I can see that there is a generated symbol for every template function instance, but every such instance that has the same number of arguments and the arguments are the same byte-size are just linked to one function.
While I can understand why this may happen, but is there a way to force each function to be standalone?
The issue was indeed COMDAT folding. Turning off the OPT:ICF and OPT:REF flags did the trick. Although worth noting that compiling with VS 2017 with the flags produced more "sensible" results than compiling the same code with VS 2013.

Template template in c++11 error

I've been reading the chapter 5 of "C++ Templates, The complete guide", and i've seen the concept of "template template" templates, so tried myself.
In a template class, i've declared my template like this:
template <typename TipoClave, typename TipoDato,
template <class Elem, class Alloc = allocator<Elem>> class Lista = vector>
And this works. My problem comes when I try to use a different container than the default.
My class is as follows:
class Tabla
{
public:
struct Celda {
TipoClave clave;
TipoDato dato;
};
Tabla(unsigned tam)
{
t.resize(tam);
}
///< Some other functions
private:
typedef Lista<Celda> ListaDatos;
Lista<ListaDatos> t;
};
And then, when I try to use it from the main program like this:
int main (void)
{
Tabla<string,Alumno,array> tabla(200);
///< Some stuff
return 0;
}
But this line Tabla<string,Alumno,array> tabla(200); does not compile, getting me an error like:
test_tabla_t.cpp: In function ‘int main()’: test_tabla_t.cpp:20:27:
error: type/value mismatch at argument 3 in template parameter list
for ‘template class Lista> class Tabla’ Tabla
tabla(200);
I've tried to use Tabla<string,Alumno,vector> tabla(200);, and it works, so I don't know how to solve this error.
Assuming you're trying to use std::array, it does not take an Allocator template parameter. Its second argument is the size of the array.
template<
class T,
std::size_t N
> struct array;
You haven't mentioned how experienced you are with C++, but since you're trying to learn more, I'll say that I've been using C++ for nearly a decade and can count on one hand the number of times I've used template template parameters.
In this case, you want Lista to be "something that acts like a sequence container[1]", you might like to read about concepts[2], which may make it into C++20. They allow you to tell the compiler the interface you want a template parameter to have.
In the meantime, it might be easier to declare Lista as a plain template parameter.
[1] http://en.cppreference.com/w/cpp/concept/SequenceContainer
[2] https://en.wikipedia.org/wiki/Concepts_(C%2B%2B)

Code explanation of the json11 library about implicit constructor

I'm reading the source code of the main json11 header file.
It contains the following declaration:
template <class T, class = decltype(&T::to_json)>
Json(const T & t) : Json(t.to_json()) {}
I'm trying to find some documentation about this usage of decltype and class inside a template declaration but no success.
Does this construction/usage has a name in C++? Any good reference about it?
It's using SFINAE ("Substitution Failure Is Not An Error"), a common technique for advanced template stuff. In this case, it's used as a crude(1) test whether the type T has a function named to_json.
How it works: if the expression T::to_json is well-formed (there is something named to_json inside the type T), decltype(T::to_json) denotes a valid type and the constructor template can be used normally.
However, if T::to_json is ill-formed (i.e. if there is no to_json member inside T), it means substituting the template argument for T has failed. Per SFINAE, this is not an error of the entire program; it just means that the template is removed from further consideration (as if it was never part of the class).
The effect is thus that if type T has a member to_json, you can use an object of type T to initialise a Json object. If there is no such member in T, the constructor will not exist.
(1) I'm saying crude test because this only checks that T has such a member. It doesn't check that the member is a function which can be invoked without arguments and returns something another constructor of Json can accept. A tighter-fitting test might look something like this:
template <class T, class = std::enable_if_t<std::is_constructible<Json, decltype(std::declval<const T>().to_json())>::value>>
Json(const T & t) : Json(t.to_json()) {}
[Live example]

Resources