template specialization
If you want to test whether someone has understood how template specialization works is the following which is derived from a famous example by Peter Dimov and Dave Abrahams. So let's test you!
What's the return value of the function g in the following code? Explain your opinion!
template<typename T> int f(T) { return 1; }
template<> int f<>(int*) { return 2; }
template<typename T> int f(T*) { return 3; }
int g(int* i) { return f(i); }
geeky Hello World application
Today's C++ puzzle was originally developed together with my student assistant Juergen as a geeky idea on how to write a full blown Java application without a main method. The same "technology" does work on C++ as well but you have to fake the linker with a symbol main to translate the program. You can do this by providing a global integer variable main instead of a main method.
Now for today's puzzle: Write a Hello World application without a function main. What is the drawback of this "technology" to write applications, apart from the obvious one that it is a ugly hack?
finally clause in C++
travel, meeting, snow, and broken code
Today I finally booked my travel to Brussels next month. In the afternoon I had a refreshing meeting of the faculty council. In the evening it started snowing a bit and thus I designed the following broken code:
namespace n {
class c;
void f(c&);
};
void f(n::c& v) {
f(v);
}
Explain why this is broken code in C++.
The first public openSUSE IRC team meeting was held yesterday and went - in my opinion - very well.
deducing types from code
One of the ugliest things in C++ is reading complex type names. It is even uglier to deduce types from ugly code. It gets worst if templates are involved as well. We skip templates here for a first shot. Watch the correct(!) C++ statement 0[new weird](1)()(3,"strange")((weird(*)())0,5); and try to understand the syntactic meaning. Next describe the signature of the class weird that contains one method only.
I hope you will never _write_ code like that but you most likely will _read_ similar code if you have to work with code from other people. --- This is not a joke but a horror!
deemed to fail
On Mondays people often write broken code. Actually they do it the other days as well but this leads to the following C++ puzzle for today.
The following code is broken and any instantiation of an object of a class derived from this abstract class is deemed to fail. Explain why this is the case. Note that your first thought might be wrong!
struct broken {
virtual void v() = 0;
void n() { v(); }
broken() { n(); }
};
casting from base classes
After visiting Schwetzingen I designed the following C++ question for today:
C++ has two reasonable options to cast from a base class to a sub class. These options are the static_cast operator and the dynamic_cast operator. Explain the differences between these two operators and compare their advantages and disadvantages.
Explain why one cannot use the dynamic_cast operator to cast from a base class that does not have a virtual function.
Explain why one cannot use the static_cast operator to cast from a virtual base class.
QtCanvas
Finally qtcanvas classes are available for Qt 4 series
This isn’t the final version of qtcanvas, it’s only a backport of the original classes shipped with Qt3.
So what’s the difference between this qtcanvas and qt3canvas (available only through Qt3 support with Qt4)? Simple this version works with all open-source versions of Qt >= 4.1.0!!
In this way you can use qtcanvas also under windows (before it wasn’t possible with the open-source edition).
I’ve tried qtcanvas under Mac OS X Tiger, Gnu-Linux and Windows XP and they work fine.
Gentoo documentation checker
gen-docheck is a useful tool for the gentoo italian translation team. gen-dockeck compares the version number of english document and italian translation.
In this way you can watch the status of one or more guides, keeping the translations updated.
Features:
- mail notification support (straight to guide’s translator or to a specified address)
- filter guides using regular expressions
Requirements:
gen-dockeck requires:
- perl
- perl module LWP::Simple (under debian is provided by libwww-perl)
- perl module Net::SMTP
- perl module Getopt::Long (usually available by default with all perl installation)
- perl module Pod::Usage (usually available by default with all perl installation)
Synopsis:
gen-docheck syntax: gen-docheck [--help] [--man] [--config configuration
file] for more informations read the man page: gen-docheck --manan
Configuration file:
gen-docheck support also configuration files.
This is an example:
#mail sender
sender = gentoo_doccheck@gentoo.orgThis email address is being protected from spam bots, you need Javascript enabled to view it
#check only guides mathing these names (use "." to match all, "," to separate names)
checkonly = diskless,macos
#checkonly = .
#send mail notify to translator
mailnotify = 0
#send all mail notify to this address
force_mail_destination = flavio.castelli@gmail.com
# smtp server
smtp = smtp.tiscali.it
# debug smtp commands
smtpdebug = 0
Usage:
You can automate gen-docheck adding it to cron.
Here’s an example:
0 10 * * 0 /home/micron/gen\-docheck/gen\-docheck.pl --config /home/micron/gen\-docheck/gen\-docheck.conff
In this way you’ll run gen-docheck every sunday at 10:00 AM
Download
The code can be found inside of this git repository.