Hi all,
Please forgive me if this question is already asked, but i cant find anything related to it. This is my question;
Is there anything equivalent in freebasic like template in c++ ? This is a sample code for templates in cpp.
#include <iostream>
using namespace std;
// One function works for all data types. This would work
// even for user defined types if operator '>' is overloaded
template <typename T>
T myMax(T x, T y)
{
return (x > y)? x: y;
}
int main()
{
cout << myMax<int>(3, 7) << endl; // Call myMax for int
cout << myMax<double>(3.0, 7.0) << endl; // call myMax for double
cout << myMax<char>('g', 'e') << endl; // call myMax for char
return 0;
}
Dim MyInt As integer=123456
Dim MySingle As Single=123.456
Dim MyDouble As Double=123.456
print "MyInt= ";MyInt
print "MySingle=";MySingle
print "MyDouble=";MyDouble
sleep
kcvinu wrote:Hi all,
Please forgive me if this question is already asked, but i cant find anything related to it. This is my question;
Is there anything equivalent in freebasic like template in c++ ?
No, unfortunately generics aren't implemented yet, as of version 1.06.0.
@paul doe,
Thanks for the reply. It is very useful.
Assume that we are writting a function for adding elements to an array instead of calling Redim each time.
And we want to use this same function for all supported data types in fb. But now, one would need to write separate function for each datatypes. But if we have template in fb, then it wont be a problem anymore.
As paul already mentioned FB has no generics currently. You can use macros to get a similar functionality for simple scenarios, but of course it's no replacement for generics.
Function myMax Overload (Byref x As Integer, Byref y As Integer) Byref As Integer
If x > y Then
Return x
Else
Return y
End If
End Function
Function myMax Overload (Byref x As Double, Byref y As Double) Byref As Double
If x > y Then
Return x
Else
Return y
End If
End Function
Function myMax Overload (Byref x As Zstring, Byref y As Zstring) Byref As Zstring
If x > y Then
Return x
Else
Return y
End If
End Function
Print myMax(3, 7)
Print myMax(3.0, 7.0)
Print myMax("g", "e")
Sleep