User Tools

Site Tools


c_-_c_templates:class_template_specialization

C - C++ Templates - Class Template specialization

#include <iostream>
using namespace std;
 
template <class T> 
class specific {
  T value;
 
public:
  specific(T a) {
    cout << "Generic specific\n";
    value = a;
  }
 
  T getvalue() {
    return value;
  }
};
 
 
// Explicit specialization for float.
template <> 
class specific<float> {
  int value;
 
public:
  specific(float a) {
    cout << "Specific<char> specialization\n";
    value = a * a;
  }
 
  int getvalue() {
      return value;
  }
};
 
 
int main()
{
  specific<char> d('a');
  cout << "Character value: " << d.getvalue() << "\n\n";
 
  specific<float> i(2.2);
  cout << "Float value: " << i.getvalue() << "\n";
 
  return 0;
}
c_-_c_templates/class_template_specialization.txt · Last modified: 2020/07/15 09:30 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki