Okey, here's an example with variadics. Of course, that's not ever the best way, in fact, it's absolutely bad practice but I was just playing with.
So, if you are interested in what this guy did? go ahead if not and you want to learn and even use
some of my ideas, don't try it.
Being weighed, this is not a good example, just take it for fun.
Output:
Thursday, March 29, 2012
Wednesday, March 28, 2012
Variadic templates
Every time, every time, C++ surprise me. Today, with variadic templates that allow us to specifier an unknown number of arguments. And this becomes an completely useful tool.
At the moment, I won't explain it because still being a mystery for me. Just say that this allow recursion
like prolog. Moreover, we can make containers of different types and even very things that I don't know.
Here's my example:
Output:
At the moment, I won't explain it because still being a mystery for me. Just say that this allow recursion
like prolog. Moreover, we can make containers of different types and even very things that I don't know.
Here's my example:
Output:
String arguments in templates
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.
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.
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.
Monday, March 5, 2012
Pointers and Constants using Int
Hello everybody,
I did a short example to give an expression of pointers and constants. It's uniquely illustrative. With all this, I'm trying to lay the groundwork to const correctness's use because the good use of const is absolutely important in complex applications where the efficient matter.
Here we go:
I did a short example to give an expression of pointers and constants. It's uniquely illustrative. With all this, I'm trying to lay the groundwork to const correctness's use because the good use of const is absolutely important in complex applications where the efficient matter.
Here we go:
Subscribe to:
Posts (Atom)