Archive for August, 2004

Very brief intro to C++ template specializations

August 6th 2004

I was looking through my C++ templates book today, when I realized that a lot of these things were real obscure, so there’s a lot of programmers who wouldn’t know about them unless I could help explain it.

So what I’m going to be discussing is template specialization. Hopefully if you program in C++, you’re familiar with templates. They are a generic programming facility for C++, that can be applied to classes and functions. One of the more notable differences from normal classes is that templates must be defined in the header file where they’re declared.

template
class Foo {
   public:
   Foo(const T& thingy) : m_thingy(thingy) { }

   private:
   T m_thingy;
}; // class template

template
T max(const T &a, const T &b)
{
   return (a > b) ? a : b;
} // function template

Well, it turns out that you’re able to special-case code for a given type. For example, if you know you can do a better job implementing max(), as long as it’s working with an int, you can specialize max for that type, as follows:

template
int max(const int &a, const int &b)
{
   // Do your best pardner
}

You can also specialize templated classes. There’s two ways you can do so:

  1. Full specialization
  2. Member specialization

Before I explain it, let me give you an example class I’ll be using:

template
class Outer {
    public:
    void print(const T &value) const {
        cout 

In full specialization, you redeclare the template class definition, but leave the space in between the template brackets blank, and add a bracketed definition to the end of your class name.  You then replace all of the type names in your class definition with whatever type you're specializing for.  You must do this for every method in the class.


// Specializing for bool, you must specialize all members!
template
class Outer {
    public:
    void print(const bool &value) const;
    void name() const; // We *have to* declare this too.
};

When defining the methods in this fashion, you don’t precede the function with template , because your fully specialized class is no longer a template. Since your class is no longer a template, you can declare it in a header, and define it later in a .cpp, just like for normal classes.

// Don't precede with template, as this function isn't part of a template.
void Outer::print(const bool &value) const
{
    cout ::name() const
{
    cout 

Now in member specialization, you don't bother re-defining the class.  You just define the methods.  This time you do have to precede with template .


// Full member specialization.  After this declarations, you can no longer
// provide a full specialization for Outer.  Notice we don't declare
// a class here, just some functions.  Notice also that we DO precede with
// template this time.
template
void Outer::print(const int &value) const
{
    cout 

Note that both of these methods fail if you've already managed to have the generic form instantiated for that type, by declaring a variable using it.  If you preceded Outer::print() with a Outer outVar; declaration, you'll have problems.

Also, note that when doing member specialization, you can usually just declare a non-template function with the same member name and arguments.  In C++, a non-template function with the same signature as a template function will win in overload resolution.

Now if you go ahead and make a sample program using these classes (don't
forget to include  and your using namespace std;
commands), you can see how it works.


// Notice that we didn't declare the name method again.  Instead, the generic
// version will be used.
int main(void)
{
    Outer f;
    Outer b;
    Outer i;

    f.print(3.0);
    f.name();

    b.print(false);
    b.name();

    i.print(2);
    i.name();

    return 0;
}

Building and running this program gives me the following output:

Hello from generic-ville!
Generic Outer class.
bool value is equal to 0
bool-specialized Outer class.
int value is equal to 2
Generic Outer class.

BTW, The book I was reading is C++ Templates: The Complete Guide by Vandevoorde and Josuttis. It’s really a handy reference to have around for when templates are kicking your ass. =D

Posted by mpyne under Uncategorized | No Comments »

Lil’ Brudder

August 6th 2004

From Homestar Runner

Please don’t flame me, you really need to see the clip to put it in context.

Posted by mpyne under Uncategorized | No Comments »

Working

August 5th 2004

I’m working now on an idea I have to refactor JuK’s playlist code to make the previous/next track decision code independent of the playlist code. The idea being to finally add support for the #2 requested feature in JuK, the ‘upcoming’ playlist.

The way I see it, this kind of thing is only useful in the sense that you can queue music to be played, and the music is automatically removed from the queue when it’s played. What I’m planning right now is that when the upcoming playlist is enabled, the track decision code would defer to the upcoming playlist, otherwise everything proceeds as normal. As the tracks get played from the upcoming playlist, they get removed. When the playlist is empty, playback stops.

One possibility is that the upcoming playlist can automatically keep at least foo amount of tracks queued up, so you can review the songs you’re about to hear without having to click Next when you hear a song you don’t like. I’ve still not decided how I would implement that, although it seems to me that this would only be useful for Random Play.

So yeah, that’s what I’ve been up to.

Posted by mpyne under Uncategorized | No Comments »

Yay!

August 4th 2004

Well, Scott and I managed to get JuK down below 10 bugs in time for KDE 3.3 RC1, and I am insanely happy, especially since about half of the bugs seem to be unreproducable, and the other half can’t be fixed for 3.3 for the most part.

I’ve been forming a TODO list of things I want to work on for JuK for 3.4/4.0. Now that we’re in a more or less total freeze, I can probably start working on the easy ones now so they’ll be ready when the devel tree branches.

Just remember though, even though I’m happy that JuK is below 10 bugs, if you find a bug, feel free to report it! I can’t fix it if I don’t know it’s broken! ;-)

Posted by mpyne under Uncategorized | No Comments »

Got a new car

August 3rd 2004

No one’s blogged recently, so I figured I’d regale you with tales of the car my wife and I bought yesterday. It’s a

2004 Chevrolet Aveo LS. It’s apparently a rebadged Daewoo, but that’s OK, the car has been very nice from my day or so of using it.

It includes an MP3-playing CD radio, remote lock/unlock (built into the key no less), and is just very nice all around. It’s very small (for an American car), but that was the reason I bought it, as it’s fuel-efficient and oh-so-easy to park. Of course, I showed it to my mother, and she’s all like, “Where’s the rest of it??”

We went to a smaller car dealership, and got generally good service. I got all confused with the APR rate, and accepted financing at the dealership when I could’ve got a better deal at my credit union. I shouldn’t be too surprised, as this is my first major credit buy of any sort, and still ended up with a decent APR (mine’s 11%, my mother’s is 22%). Luckily after I mentioned I’d probably re-finance the loan, the dealership finagled with GMAC (GM’s credit branch) to lower my APR to 7.1%, with my wife and I find acceptable.

One thing which I like about the car is that is almost impossible to lock your keys in it. Although it has power locks, there are no power lock switches on the inside, every lock is a push-lock at the window base. The catch is that the driver’s side lock won’t push down if the door is open. You have to lock the driver’s side door using either the remote (built-in to the key), or by using the keyhole (on the outside). The keyhole convieniently locks every door as well. In my experiments the only way I found to lock the keys in the car was to use the passenger door, which has no such door lock protection.

So far I’d rate the car itself as an 8.5/10. It was a tad pricey for my taste, but it was in the price range was had been budgeting all along, so I’m OK with it.

Posted by mpyne under Uncategorized | No Comments »

Some quiz results

August 2nd 2004

Edit: Changed the img tags to hopefully make the images show up on Planet KDE. They sort of give context to the rest of my blog. ;-)

Edit 2: Nope. You can click on the title above to view the blog from my site though.

What Literature Classic are you?
The name of the rose
Umberto Eco: The Name of the Rose. You are a
mystery novel dealing with theology, especially
with catholic vs liberal issues. You search
wisdom and knowledge endlessly, feeling that
learning is essential in life.

Which literature classic are you?
brought to you by Quizilla

I find this funny, because I have no clue whatsoever what this book is, although the conclusion sounds close enough.

What Type of Social Entity are You?
The Musketeer
Category IV - The
Musketeer

You have a small, highly edited social group, and
you like it that way.

What Type of Social Entity are You?
brought to you by Quizilla

I thought this one was rather unfair. It didn’t have an option for “Yes, I have one best friend and that’s pretty much it, but I’m actually quite content with that”. :-P

clee, I’m for (almost) anything to improve KDE’s build system (and build speed). As I discussed in IRC, I wouldn’t mind the /admin build system if we could centralize so that you only had one copy installed on your computer, and KDE applications wouldn’t have to distribute their own /admin. However, I like pkg-config a lot, so if you can make it work, then by all means, go for it.

And I would like to know where Timothy Butler pulled a figure of 200 KControl modules from. aseigo has already posted a nice reply to the article, so I can see no reason to elucidate further.

Posted by mpyne under Uncategorized | No Comments »

dotNET

August 1st 2004

No, this isn’t about Mono. I just wanted to let clee know that his dotNET style works a lot better now thanks to SadEagle. clee and I agreed that if he managed to fix the horrible horrible button sizing problems with dotNET that I would have to use it for one week.

Of course, that’s like someone saying they’d pay me to hack on JuK; I want to use dotNET anyways. So I think I ended up with the better part of this deal. Now to go see if I can sell snow to some Eskimos. :-D

Posted by mpyne under Uncategorized | No Comments »

KolourPaint

August 1st 2004

I am dedicating this blog entry to letting the world know that Clarence Dang’s KolourPaint, due to be included in KDE 3.3, is a very kick-ass painting program. Mind you, it’s not the Kimp, but it is much much better than KPaint ever was. And unlike MS Paint, it supports such niceties as transparency, and a selection of image effects (not just Resize / Skew). Also, there’s no Settings dialog; it doesn’t need one. :-)

You can’t make a hackergotchi head in it, but it is suitable for a great many imaging tasks that I formerly had to fire GIMP up for. Thanks, Clarence!

Posted by mpyne under Uncategorized | No Comments »

« Prev