• Saves Valuable Time
  • Trusted Accuracy for 12 Years
  • 15-Day Money-Back Guarantee

C++ Equivalent to VB Generics

VB generics and C++ templates are implemented in totally different ways - VB generics is a runtime feature, while C++ templates result in separate classes created at compile-time, but you can still often achieve the same result by converting one to the other.

Creating a List

VB.NET C++
Dim myVar As New List(Of Integer) std::vector<int> myVar;

Creating a Dictionary

VB.NET C++
Dim myVar As New Dictionary(Of String, Integer) std::unordered_map<std::string, int> myVar;

Defining a Generic Class

VB.NET C++
Public Class GenericClass (Of T) template<typename T>
class GenericClass

Defining a Generic Class with a Constraint

VB.NET C++
Public Class GenericClass (Of T As SomeBase) #include <type_traits>

template<typename T>
class GenericClass
{
    static_assert(std::is_base_of<SomeBase, T>::value, "T must inherit from SomeBase");
};

Defining a Generic Class with a 'new' Constraint

VB.NET C++
Public Class GenericClass (Of T As New) #include <type_traits>

template<typename T>
class GenericClass
{
    static_assert(std::is_default_constructible<T>::value, "T requires default-constructible elements");
};

Defining a Generic Method

VB.NET C++
Public Function Compare(Of T)(param1 As T, param2 As T) As Integer public:
template<typename T>
int Compare(T param1, T param2)

Defining a Generic Method with a Constraint

VB.NET C++
Public Sub Swap(Of T As Class)(ByRef l As T, ByRef r As T) #include <type_traits>

public:
template<typename T>
void Swap(T &l, T &r)
{
    static_assert(std::is_class<T>::value, "T must be a class");
}


Use VB to C++ Converter to convert from VB to C++.     

Additional resource: VB.NET and C++ Equivalents


Copyright © 1997 – 2017 Tangible Software Solutions Inc.