mercredi 21 février 2018

Is it common practice to not define the base template case of a function? [duplicate]

This question already has an answer here:

The mindset is I only want to specialize a function for certain types. Is it common to just leave the base-template case empty or is there a better way?

#include <iostream>

template<typename T>
void templated_function();

template<>
void templated_function<int>() {
  std::cout << "int template\n";
}

template<>
void templated_function<double>() {
  std::cout << "double template\n";
}

int main() {
  templated_function<int>();
  templated_function<double>();
}

I also came across deleted functions. Is this used very often in the context of templates?

#include <iostream>

template<typename T>
void templated_function() = delete;

template<>
void templated_function<int>() {
  std::cout << "int template\n";
}

template<>
void templated_function<double>() {
  std::cout << "double template\n";
}

int main() {
  templated_function<int>();
  templated_function<double>();
}

Aucun commentaire:

Enregistrer un commentaire