November 20, 2009
Ok, c++ tuples, here is quick and dirty:
// g++ 4.1
#include <string>
#include <iostream>
#include <tr1/tuple>
using std::tr1::tuple;
using std::tr1::tie;
using std::tr1::make_tuple;
tuple<std::string,int> parse_url(const std::string& url){
return make_tuple("127.0.0.1",9000); //Cheat for parsing
}
int main(){
std::string host;
int port;
tie(host,port) = parse_url("127.0.0.1:9000");
std::cout << "Host: "<<host<< ":"<<port<<"\n";
}
At the first look, returning several values from a function/method in C++ may look awkward and contrary to the C++ way, being more usual in languages like python or perl.
However the fact is that template metaprogramming gives us the ability to do such good tricks and renders the rest for us in compile-time. This tuple templates are part of the TR1 and thats why we need to include them from <tr1/tuple> and use them from std::tr1 namespace, but they’ll be part of the next standard c++0x.
The logic behind the metaprogramming is simple, by the time you compile a tuple the compiler knows perfectly the size of the new type because you supply the template parameters.
tie() for example is a template that gets the given parameters and put references to them in an lvalue obj that can be assigned to, so its a painless way of getting results from a tuple_returning method.
make_tuple() goes the same way but creates a new tuple with copies of the provided parameters, so it is a quick way of creating tuples of any size, its like the current make_pair() but for tuples.
Leave a Comment » |
Uncategorized | Tagged: C++, g++, gcc, make_tuple, tie, tuple |
Permalink
Posted by Arkaitz Jimenez
November 8, 2009
Just buy the “edit css” improvement and add the next lines to your css to get a wider theme, you’ll lose the shadows on the sides though.
#pagebar { width:955px; }
#page { width:955px; }
#content { width:700px; }
#sidebar { margin:-60px 0 0 750px; }
#headerimg {
background-image:url('http://s2.wordpress.com/wp-content/themes/pub/contempt/images/blue_flower/head.jpg');
background-repeat:repeat-x;
}
#footer {width:955px; }
EDIT: Thanks to Ivan Zahariev for the tip on keeping the side shadows
2 Comments |
Uncategorized | Tagged: contempt, css, wider, wordpress.com |
Permalink
Posted by Arkaitz Jimenez
November 7, 2009
Often when looking in the internet for implementations of some patterns in C++ we come across the well-known Meyers singleton, which is basically the next code:
class Singleton{
Singleton(){
// Thread-unsafe code
}
static Singleton* getInstance(){
static Singleton instance;
return &instance;
}
};
This essentially reserves static memory for the size of the instance and executes the class constructor during the first call to getInstance(), this of course occurs only if we are dealing with a non-POD object.
The problem here is that we might run in problems if we get inside getInstance with 2 threads at the same time for the first time. Obviously the constructor is not thread-safe per se.
However, GCCs g++ gives us a help here and protects every local static variable construction with thread-safe guards, __cxa_guard_acquire and __cxa_guard_release do the job here protecting the constructor. Note that this only happens with local statics as global static construction occurs before any function of the translation unit gets executed.
Differences are obvious if we take a look at the generated code, PODs are just data so no construction is needed, non-PODs are properly protected
POD type, function body highlighted:
class pod_class { public:
static pod_class* instance(){
static pod_class once;
return &once;
}};
int main(){ return (int)pod_class::instance; }
080483fd <_ZN9pod_class8instanceEv>:
80483fd: 55 push %ebp
80483fe: 89 e5 mov %esp,%ebp
8048400: b8 f8 95 04 08 mov $0x80495f8,%eax
8048405: 5d pop %ebp
8048406: c3 ret
Non-POD type, class constructor and function body higlighted:
Read the rest of this entry »
4 Comments |
Uncategorized | Tagged: C++, gcc, meyers singleton, singleton, static local, threadsafe, __cxa_guard_acquire |
Permalink
Posted by Arkaitz Jimenez
September 18, 2009
Have you heard that a new c++ draft is coming soon? It’s called c++0x for the moment and includes some new cool features.
A good description of the new features.
Basics
I’ve been recently playing around with lambda function support and looks pretty stable in the gcc compiler. I haven’t tried but I read that VC2010 has lambda support as well.
It gives support for in-place anonymous function definitions with context access, which basically helps us in a lot of situations, for example, if a given function, say wait_5minutes_call(callback_function), requires a callback we could easily do:
wait_5minutes_call( [ ](){ std::cout << "Callback called\n";} );
And thats all!! cool isn’t it?
A more complete example:
#include <iostream>
#include <functional>
#include <algorithm>
void print_function(int size, std::function funct){
std::cout << funct(size) << std::endl;
}
int main(int argc, char* argv[])
{
auto myfnc = [](int size){return size*size;};
print_function(10,myfnc);
return 0;
}
The syntax is:
Read the rest of this entry »
Leave a Comment » |
Uncategorized | Tagged: C++, c++0x, gcc, lambda |
Permalink
Posted by Arkaitz Jimenez