Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template member function specialization in a template class

I have a template class and a member function print() to print the data.

template<typename T>
class A
{
public:
   T data;
   void print(void) 
   { 
      std::cout << data << std::endl; 
   }
   // other functions ...
};

Then, I want to either print scalar data or vector data, so I give a specialized definition and get a compiler error.

template<typename T>
void A<std::vector<T>>::print(void) // template argument list error
{
   for (const auto& d : data) 
   {
      std::cout << d << std::endl;
   }
}

Question: Why does this member function specialization get an error? What is the correct way to define a print function for a vector?

Solution 1: I have tested the following definition.

template<typename T>
class A<std::vector<T>>
{
public:
   std::vector<T> data;
   void print(void) {   // OK
      // ... 
   } 
}

This one worked, but I have to copy the other member functions into this specialized class.


EDIT:

Solution 2: To prevent copy all the other member functions, I define a base class containing the common member functions and inherit from the base class:

template<typename T>
class Base
{
public:
   T data;
   // other functions ...
};

template<typename T>
class A : public Base<T>
{
public:
   void print(void) 
   {
      std::cout << this->data << std::endl;
   }
};

template<typename T>
class A<std::vector<T>> : public Base<std::vector<T>>
{
public:
   void print(void) 
   {
      for (const auto& d : this->data)
      {
         std::cout << d << std::endl;
      }
   }
};

This solution works well. Are there some better or more conventional solutions?

like image 398
kfckfckf Avatar asked Jul 06 '20 07:07

kfckfckf


People also ask

What is function template specialization?

The act of creating a new definition of a function, class, or member of a class from a template declaration and one or more template arguments is called template instantiation. The definition created from a template instantiation is called a specialization. A primary template is the template that is being specialized.

Can a member function be a template?

Member functions can be function templates in several contexts. All functions of class templates are generic but are not referred to as member templates or member function templates. If these member functions take their own template arguments, they are considered to be member function templates.

What is the specialty of a template function give example?

Template in C++is a feature. We write code once and use it for any data type including user defined data types. For example, sort() can be written and used to sort any data type items. A class stack can be created that can be used as a stack of any data type.

Can a non template class have a template member function?

A non-template class can have template member functions, if required. Notice the syntax. Unlike a member function for a template class, a template member function is just like a free template function but scoped to its containing class.


1 Answers

Why does this member function specialization get error?

When you instantiate the template class A for example A<std::vector<int>>, the template parameter T is equal to std::vector<int>, not std::vector<T>, and this a specialization case of the function. Unfortunately this can not be done with member functions as mentioned in the comments.


Are there some better solutions?

Yes; In c++17 you could use if constexpr with a trait to check the std::vector, like this.

#include <type_traits> // std::false_type, std::true_type
#include <vector>

// traits for checking wether T is a type of std::vector<>
template<typename T> struct is_std_vector final : std::false_type {};
template<typename... T> struct is_std_vector<std::vector<T...>> final : std::true_type {};

template<typename T>
class A /* final */
{
    T mData;

public:  
    // ...constructor  

    void print() const /* noexcept */
    {
        if constexpr (is_std_vector<T>::value) // when T == `std::vector<>`
        {
            for (const auto element : mData)
                std::cout << element << "\n";
        }
        else // for types other than `std::vector<>` 
        {
            std::cout << mData << std::endl;
        }
    }
};

(See Live Online)

This way you keep only one template class and the print() will instantiate the appropriate part according to the template type T at compile time.


If you don not have access to C++17, other option is to SFINAE the members(Since c++11).

#include <type_traits> // std::false_type, std::true_type, std::enbale_if
#include <vector>

// traits for checking wether T is a type of std::vector<>
template<typename T> struct is_std_vector final : std::false_type {};
template<typename... T> struct is_std_vector<std::vector<T...>> final : std::true_type {};

template<typename T>
class A /* final */
{
    T mData;

public:  
    // ...constructor  

    template<typename Type = T> // when T == `std::vector<>`
    auto print() const -> typename std::enable_if<is_std_vector<Type>::value>::type
    {
        for (const auto element : mData)
                std::cout << element << "\n";
    }

    template<typename Type = T> // for types other than `std::vector<>`
    auto print() const -> typename std::enable_if<!is_std_vector<Type>::value>::type
    {
        std::cout << mData << std::endl;
    }
};

(See Live Online)


What if I have more other data types like self-define vector classes or matrices? Do I have to define many is_xx_vector?

You can check the type is a specialization of the provided one like as follows. This way you can avoid providing many traits for each type. The is_specialization is basically inspired from this post

#include <type_traits> // std::false_type, std::true_type
#include <vector>

// custom MyVector (An example)
template<typename T> struct MyVector {};

template<typename Test, template<typename...> class ClassType>
struct is_specialization final : std::false_type {};

template<template<typename...> class ClassType, typename... Args>
struct is_specialization<ClassType<Args...>, ClassType> final : std::true_type {};

And the print function could be in c++17:

void print() const /* noexcept */
{
   if constexpr (is_specialization<T, std::vector>::value)// when T == `std::vector<>`
   {
      for (const auto element : mData)
         std::cout << element << "\n";
   }
   else if constexpr (is_specialization<T, ::MyVector>::value)  // custom `MyVector`
   {
      std::cout << "MyVector\n";
   }
   else  // for types other than `std::vector<>` and custom `MyVector`
   {
      std::cout << mData << std::endl;
   }
}

(See Live Online)

like image 190
JeJo Avatar answered Oct 19 '22 11:10

JeJo