Код: Выделить всё
#pragma once
template
T add(T, T);
< /code>
add.cpp
#include "add.hpp"
template
T add(T a, T b) {
return a + b;
}
// Only supported types
template int add(int, int);
template unsigned add(unsigned, unsigned);
template float add(float, float);
template double add(double, double);
< /code>
main.cpp
#include
#include "add.hpp"
int main() {
printf("Output is: %d\n", add(1,1));
printf("Output is: %f\n", add(1,3));
// I want it to raise a compiler error, not linker error
printf("Output is: %d\n", add(0,1));
return 0;
}
Если шаблоны не являются инструментом для этого, то что? Я не хочу выполнять перегрузку функции, потому что я не хочу повторять код в add () .
Подробнее здесь: https://stackoverflow.com/questions/796 ... e-hiding-i