I'm confused by these three things. Here is a simple example:
template<typename T>
void func(T t) {
if (typeid(T) == typeid(int)) {
std::cout << "f - int" << std::endl;
} else {
std::cout << "f - other" << std::endl;
}
}
template<typename T>
void func2(T t) {
if (std::is_same<T, int>::value) {
std::cout << "f2 - int" << std::endl;
} else {
std::cout << "f2 - others" << std::endl;
}
}
template<typename T>
void func3(T t) {
if constexpr (std::is_same<T, int>::value) {
std::cout << "f3 - int" << std::endl;
} else {
std::cout << "f3 - other" << std::endl;
}
}
int main() {
func(1);
func('a');
func2(1);
func2('a');
func3(1);
func3('a');
return 0;
}
The output is
f - int
f - others
f2 - int
f2 - others
f3 - int
f3 - others
So it works as expected. But I kind of don't know which one should be used in which case.
As my understanding, typeid
in the first one is totally about runtime. That's all I know. But template is about compile time, right? So does it mean that the func
is a stupid design?
How about the func2
and the func3
? Are they exactly the same thing? Are they all about compile time? Or the func2
is still about runtime?
Aucun commentaire:
Enregistrer un commentaire