I have developed a templated library for some computations that I need to do. Now, I need to use it in conjunction with Ceres and I want to be able to use the automatic derivaties tool. The requirement for this is that the functions and classes used need to be templated so Ceres can use its ceres::Jet<T,n> type. I have been able to develop functions and use them and ceres for solving a minimization problem. However, my library (which consists of a good amount of functions and classes) have some functions like (please, note that this is a simplified example that produces the same error as the one I am having with ceres::Jet<T,n> in my library):
template <typename T>
void func()
{
T a = 2;
}
I created a small structure that emulates the cres::Jet<T,N> one (this example struct has the same constructors as ceres::Jet<T,N>):
template <typename T, int N>
struct A
{
enum
{
DIMENSION = N
};
A()
{
}
explicit A(const T& value)
{
}
A(const T& value, int k)
{
}
};
Whenever I do func<A<double,2>>() (or any T and N) I get the following error:
error: no viable conversion from 'int' to 'A<double, 2>'
T a = 2;
^ ~
I know I can change T a = 2 to T a = T(2) and it will work. However, I don't want to change this in every place in my library (It would take a long time) unless T a = 2 is some really stupid thing that I should not be doing. I have tried to add constructors or operator= without any success (the operator= does not even compile):
template <typename M>
A(const M& a)
{
}
template <typename M>
A& operator=(const M& other)
{
return A<T, N>(T(other));
}
I have also been trying to understand my problem by reading implicit conversion and user-defined conversions but I do not understand how to use that information in my example.
My questions are:
- Is there a way that I can do that by adding some definition to the class?
- Is there a way that I can do that without adding some definition in the class?
Thank you very much.