Hello everybody,
Lately I have been snowed under work, in fact, I still. But I'm posting anyway ^ ^
Well, here's an introduction to some features of templates type traits.
Notice that this post is just an introduction and if you don't see the utility now you will see in further posts. Just say that generic programming can sometimes be not generic enough.
Starting to take a look to the standard way to check types of variables.
The standard provide some ways to do it. Although has his limitations and even isn't much effective but
allows to compare or even read the type of variables.
Doing this task I'm using typeid that return a std::type_info where is implemented some
methods like name in order to return a human-readable.
I don't know why my compiles expres int as i, I think that is not much normal because I have seen
many examples where typeid().name return the word int, not i, but in fact don't care.
Output:
Useful? Could be.
Although, as I said, it has some limitations, for example, what happens if I want to compare an
usigned int and an int, is it equal? The answer is no; false.
So, now, I want to show how we could do this task using temples. The behind idea is argument deduction and specialization.
Output:
Finally, just say that boost library provide many of this template functions.
Also, I wanted to mention to the new decltype that is introduced in the new version of C++,
this one will be a tricky form to declare variables that you don't know how, because his complexity
can be complex like templates. It's not exactly related with what I explained but I think that
may be interesting to someone.
No comments:
Post a Comment