0

i have a template class and inside i have function operator> which should call a template function from another file. How do i set the function parameters or how do i have to pass the arguments to this function.

I have removed the code from the lambda function and the cmpFn, because this should be irrelevant for this example.

This is my template class, in which the operator< calls the function cmpFn. NOTE: In a previous version of this question, i have falsely declared the function header of the cmpFn.

#include "assert.h"

template <typename T>
class myvectest
{
public:
    T *arr;
    unsigned capacity;
    unsigned current;
    myvectest()
    {
        capacity = 1;
        arr      = new T[capacity];
        current  = 0;
    }
    unsigned size() const
    {
        return current;
    }
    bool operator<(const myvectest &toCompare) const
    {
        auto mfn = [ ](auto left, auto right) -> int { return 0 };
        return cmpFn(this->arr, toCompare.arr, this->size(), toCompare.size(), mfn);
    }
}

This is the cmpFn, in which i updated the paramter *a and *b

template <typename T>
bool cmpFn(T *a, T *b, unsigned size1, unsigned size2, int (*fnEq)(T, T))
{
    return false;
}

The error message tells me, that there is no matching function call, even if there is a candidate.

.../myvectest.h:83:21: error: no matching function for call to ‘cmpFn(unsigned int* const&, unsigned int* const&, myvectest <T>::operator<(const myvectest<T>&) const [with T = unsigned int]::<lambda(auto:1, auto:2)>)’
   83 |         return cmpFn(
      |                ~~~~~^
   84 |                 this->arr,
      |                 ~~~~~~~~~~
   85 |                 toCompare.arr,
      |                 ~~~~~~~~~~~~~~
   86 |                 [ ](auto left, auto right) -> int { return 0; }
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   87 |         );
      |         ~ 

I know, that there could be other solutions, but i want to learn, how this kind of passing an array should work.

Thanks in advance, Ulf

EDIT: Note, that the myvectest->arr is from dynamic size. Since this is some kind of rebuilding the std::vector class, there is also a push_back method, which can change the size and capacity of *arr

Ulf Tietze
  • 57
  • 6
  • You don't need a pointer to an array, you need a `T *` which you can pass your array as a function (the array name is the memory address of the first position in the array). – h0tst3w Jul 04 '21 at 05:48

1 Answers1

0

You need to declare your compare function like this:

template <typename T>
bool cmpFn(T *a, T *b, int (*fnEq)(std::type_identity_t<T>, std::type_identity_t<T>))
{
    return false;
}

But std::type_identity_t is C++20 and for previous versions, you need something like this for it:

template< class T >
struct type_identity {
    using type = T;
};
template< class T >
using type_identity_t = typename type_identity<T>::type;

In addition, I have removed array size from argument a and b. You cannot deduce dynamic size in static compile time. If you need a compile time size, you can do it like this:

template <typename T, size_t SZ1, size_t SZ2>
bool cmpFn(T (&a)[SZ1], T (&b)[SZ2], int (*fnEq)(std::type_identity_t<T>, std::type_identity_t<T>))
{
    return false;
}

template <typename T, size_t SZ>
class myvectest
{
public:
    T arr[SZ];
    size_t capacity = SZ;
    size_t current;

    myvectest() = default;

    size_t size() const
    {
        return current;
    }

    bool operator<(const myvectest &toCompare) const
    {
        return cmpFn(
                this->arr,
                toCompare.arr,
                [](auto left, auto right) -> int { return 0; }
        );
    }
};
Afshin
  • 8,839
  • 1
  • 18
  • 53
  • Why do i have to remove the array size from the function parameters. [I have found this piece of code, from where i got my inspiration](https://stackoverflow.com/a/46482119/10617522). Also, when moving the function from the "assert.h" to my class with a function header like this: `bool cmpFn(const vector &toCompare, int (*fnEq)(T, T)) const` this will work. – Ulf Tietze Jul 04 '21 at 05:45
  • @UlfTietze For that code to work, array need to be defined at compile time like `int arr[20]`. **its size need to be defined at compile time**. I will update answer by a version that works like that, but you cannot have size if you want to dynamically allocate array with `new` (unless size of array to new is fixed at compile time which is somehow meaningless). – Afshin Jul 04 '21 at 05:51
  • Ah, thanks mate, that makes sense now. So i've switched my function header to this, but it still don't work. Failes with the same error message. `template bool cmpFn(T *a, T *b, int size1, unsigned size2, unsigned (*fnEq)(std::type_identity_t, std::type_identity_t))` – Ulf Tietze Jul 04 '21 at 05:54
  • @UlfTietze I have tested that code and it works. here is godbolt: https://godbolt.org/z/xEq7qbqoj remember you need C++20. – Afshin Jul 04 '21 at 06:00
  • no i don't have the size of the vector during compile time. Our professor want's to rebuild logic of the std::vector, or at least parts of it and with some custom logic. So the size of capacity can switch every time there is a user input, and therefore i don't know about the size. Thanks to you, i know that i can't do it myway, so i pass the size as integer to the function header, as you can see in my last comment. – Ulf Tietze Jul 04 '21 at 06:03
  • I would, but it still don't work. As i described in my last comment, the array must be dynamic. So sadly your information and solution is pretty helpful but not what i'm looking for. – Ulf Tietze Jul 04 '21 at 06:28