Well, well, well.
Up to this point, you should have a minimal idea of templates. Of course, there are some chapter
that you didn't understand yet or you didn't take the whole idea that I was trying to teach.
But don't worry, you just need reviews like this post to understand that some of the titles I made
are of huge interest and even, indeed, more complex.
Here there's an example that show that the chapter, argument deduction, is more complex as it
seems and we don't have to take it as if nothing because even with the string type we can get
much mistakes as you think.
First look, you may think, what the hell is happening here, how it doesn't work?, but if you take long
time and read the compiler you'll that is an error about types.
First off, remember that string is a C++ type and when you declare some thing like "this (with commas)" you are not really creating an string if not that is an char*. Thus, max(), is not
calling the function max with string's type if not that are char's.
Beside, we have to notice, in order to see the error, that the problem becomes from pass arguments
as reference (&) because without this we didn't get error from first and second call.
That's it because without pass by reference the type pass it is const char*. On the other hand, calling
by reference, we are passing the following type. char const[3] where this number is the size of
the string.
This makes that two strings of different sizes makes an error like in the second call where
"hello" is 4 and "bye" is 3(insist, all this is by argument deduction).
There are many forms to solve this problem like be sure that strings are strings(for instance, string("bye") ) but I want to handle it with templates; it's not as elegant as I'd like but this is it.
Showing posts with label Argument Deduction. Show all posts
Showing posts with label Argument Deduction. Show all posts
Wednesday, March 28, 2012
Friday, March 23, 2012
Type Traits
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.
And even functions like:
is_pointer<T>
is_arithmetic<T>
is_function<T>
is_class<T>
is_polymorphic<T>
has_trivial_constructor<T>
Awesome, I know.
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.
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.
#include <boost/type_traits>
And even functions like:
is_pointer<T>
is_arithmetic<T>
is_function<T>
is_class<T>
is_polymorphic<T>
has_trivial_constructor<T>
Awesome, I know.
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.
Subscribe to:
Posts (Atom)