I can define a specialized function in a cpp like so...
// header
template<typename T>
void func(T){}
template<>
void func<int>(int);
// cpp
template<>
void func<int>(int)
{}
How can I define a method in a specialized class in a cpp? Like so (which doesn't work, I get error C2910: 'A<int>::func' : cannot be explicitly specialized
)...
// header
template<typename T>
struct A
{
static void func(T){}
};
template<>
struct A<int>
{
static void func(int);
};
// cpp
template<>
void A<int>::func(int)
{}
It is possible in C++ to get a special behavior for a particular data type. This is called template specialization. Template allows us to define generic classes and generic functions and thus provide support for generic programming.
How many types of specialization are there in c++? Explanation: There are two types of specialization. They are full specialization and partial specialization.
Instead the programmer can write a C++ template based function that will work with all data types. There are two types of templates in C++, function templates and class templates.
The relationship between a class template and an individual class is like the relationship between a class and an individual object. An individual class defines how a group of objects can be constructed, while a class template defines how a group of classes can be generated.
Use following syntax in your .cpp
file:
void A<int>::func(int)
{
}
This is Visual C++ kinda feature.
See MSDN C2910 error description for details:
This error will also be generated as a result of compiler conformance work that was done in Visual Studio .NET 2003:. For code will be valid in the Visual Studio .NET 2003 and Visual Studio .NET versions of Visual C++, remove template <>.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With