FreeBasic equivalent of template in c++

New to FreeBASIC? Post your questions here.
Post Reply
kcvinu
Posts: 232
Joined: Oct 07, 2015 16:44
Location: Keralam, India

FreeBasic equivalent of template in c++

Post by kcvinu »

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.

Code: Select all

#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;
}
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: FreeBasic equivalent of template in c++

Post by jj2007 »

kcvinu wrote:// One function works for all data types
It's not called "template" but it exists (btw also in Masm):

Code: Select all

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
paul doe
Moderator
Posts: 1738
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: FreeBasic equivalent of template in c++

Post by paul doe »

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.
kcvinu
Posts: 232
Joined: Oct 07, 2015 16:44
Location: Keralam, India

Re: FreeBasic equivalent of template in c++

Post by kcvinu »

@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.

@jj2007,
:)
St_W
Posts: 1627
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: FreeBasic equivalent of template in c++

Post by St_W »

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.
fxm
Moderator
Posts: 12147
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBasic equivalent of template in c++

Post by fxm »

Or overload procedures:

Code: Select all

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

Code: Select all

 7
 7
g
Post Reply