- edited description
How do I use a derived type inside a function?
In the following function, I got not 1.0 but “( )” with the line with std::cout.
If I use T(1.0), I got “(-4.8367e-26 )”.
Do I miss something here?
Thanks in advance.
template <typename T>
void somefunction(blaze::Vector<T, blaze::columnVector> &x, const blaze::Vector<T, blaze::columnVector> &y)
{
.....(some code)
blaze::DynamicVector<T> temp(2 * N + 1);
temp[0] = 1.0;
std::cout << temp[0];
.....(some code)
}
Comments (2)
-
reporter -
- changed status to wontfix
Hi Kyung!
As the wiki explains the
Vector
class represents the base class for all kinds of vectors in the Blaze library.Vector
is a CRTP base class, which means that the typeT
is the type of the concrete vector (e.g.DynamicVector
,StaticVector
, ...).Considering your code example:
template< typename T > // <- This template parameter 'T' represents the concrete type of vector the function accepts void somefunction(blaze::Vector<T,blaze::columnVector> &x, const blaze::Vector<T,blaze::columnVector> &y) { // ... }
Given the following function call ...
blaze::DynamicVector<int,blaze::columnVector> a{ 1, 2, 3, 4, 5 }; // Create a 5D vector with the values (1, 2, 3, 4, 5) somefunction( a ); // 'T' now represents 'DynamicVector<int,columnVector>'
... the template parameter
T
of the function template now represents the kind of vector, i.e.DynamicVector<int,blaze::columnVector>
.Assuming that
T
isDynamicVector
, the expressionT()
creates a new empty vector, andT(1.0)
creates an uninitialised dynamic vector of size1
(hence the single element of value-4.8367e-26
).If you want to access elements of the given vectors, you have to perform an explicit cast back to the concrete vector type via the tilda operator (i.e.
operator~()
):template< typename T > // <- This template parameter 'T' represents the concrete type of vector the function accepts void somefunction(blaze::Vector<T,blaze::columnVector> &x, const blaze::Vector<T,blaze::columnVector> &y) { T temp(2*N+1); // Create another vector of type 'T' temp[0] = 1.0; // Set the element at index 0 to 1.0 (~x)[0] = (~y)[0]; // Copy an element from y to x }
For further examples, please consult the wiki.
I hope this resolves your problems. Please feel free to ask additional questions.
Best regards,
Klaus!
- Log in to comment