Download

Help with template specialization for TArray

Hi!

I’m having some trouble specializing a class if the the template itself is a TArray.
Is there a way to make the same function call receive either a TArray or a simple variable like a int32 or float and specialize it just for the TArray? I tried overloading like this:

template <typename T>
class ClassName
{
public:
	static void MethodCall(TArray<T> Variable);
	static void MethodCall(T Variable);
};

But I realized that when calling the method it would need the type of the array in the template parameter, so it won’t be the same call a needed. And this doesn’t compile because of TArray been a template itself, so the preprocessor redefines the TArray class (I think)

I tried specializing the class then, but then with the template parameter from the TArray I kind of get lost, maybe something like this:

template <typename T>
class ClassName
{
public:
	static void MethodCall(T Variable);
};

template <TArray<typename T>>
class ClassName
{
public:
	static void MethodCall(T Variable);
};

Obviously this doesn’t compile.

Thanks in advance for any help, and sorry for any english errors

This small sample works flawless


#include <iostream>
#include <vector>
using namespace std;

template <typename T>
class Test
{
public:
    static void Func(vector<T> Param1)
    {
        cout << "Func_Array" << endl;
    }

    static void Func(T Param1)
    {
        cout << "Func" << endl;
    }
};

int main(void)
{
  vector<int> vector;
  Test<int>::Func(5);
  Test<int>::Func(vector);
  return 0;
}
1 Like

Oh! It really worked, even with TArray.
The problem was my call. I was using a macro to call, with delctype to get the param:

#define CALLFUNC(Param1) (Test<decltype(Param1)>::Func(Param1))

But when the Param1 is a TArray, the template type becomes wrong.
I guess I’ll have to make a different macro for both types of call :confused:

I finally managed to do what I was intending from the beginning. I’m posting the solution so, if anyone needs it. It’s a little ugly, but worked. If anyone has a better solution, please do tell me :smiley:

#include <type_traits> // for decltype

// the macro
#define CALLFUNC(Param1) {\
if (TIsTArray<decltype(Param1)>::Value) \
    { \
        (Test<std::remove_reference<decltype((void(), Param1))>::type>::Func(Param1)); \
    } \
    else \
    { \
        (Test<decltype(Param1)>::Func(Param1)); \
    } \
}

// the main template class
template <typename T>
class Test
{
public:
    static void Func(T Param1);

};

// the specialized class
template <typename T>
class Test<TArray<T>>
{
public:
    static void Func(TArray<T> Param1);

};

And thanks to Rumbleball for helping me realize the problem was the call, not the overload