Pizer’s Weblog

programming, DSP, math

Archive for the ‘Programming’ Category

Fast Digital Sine Oscillator

with one comment

I originally intended this blog to be about math and digital signal processing as well and not just about C++. Since I recently answered a programming forum question regarding efficient generation of sine waves and took the time to code one in — surprize — C++, I thought to myself, why not sharing the code along with explanations about how it works.

If you’re interested in software synthesizers in general or generating digital representations of DTMF tones and don’t want to waste many CPU cycles when generating sine waves, keep on reading.

Read the rest of this entry »

Written by pizer

February 8, 2010 at 5:06 pm

Posted in DSP, Math, Programming

Tagged with , , , , ,

C++0x: No concepts no fun?

with 6 comments

“Concepts” was a proposed C++0x feature that was supposed to make generic programming more enjoyable. The intention was to render many guru-level “template tricks” superfluous, produce much better compile-time error messages and provide “modular type checking” so errors can be caught early (even before a template is instantiated).

  • Fact 1: Concepts have been removed from the C++0x standardization effort. See Doug Gregor’s blog post.
  • Fact 2: C++0x still includes a whole lot of other core language and library features.

Read the rest of this entry »

Written by pizer

August 16, 2009 at 6:29 pm

C++: Exploiting Copy Elisions

with 8 comments

This article is about writing efficient code and giving the compiler opportunities for optimizations with respect to copying / “moving” objects in good old C++98. I use a simple function as an example but it should extend to other cases as well. The technique that is described here (“swaptimization”) is not entirely new and I can’t take credit for it. But it is probably little-known which is a good reason to write about it. 🙂

Read the rest of this entry »

Written by pizer

June 25, 2009 at 4:57 pm

C++0x: Rvalue references 101

leave a comment »

If found a very nice article about rvalue references and move semantics (primarily) by Dave Abrahams. It also considers return value optimizations, exception safety and in what state an object should be left in after its resources have been “stolen”. Here it is.

[Edit 2009-08-18: The link doesn’t seem to work anymore. However, Dave Abrahams has started to refine, extend, and convert it into a series of blog posts on his new site ]

– P

Written by pizer

May 27, 2009 at 2:22 pm

C++0x: Do people understand rvalue references?

with 8 comments

[last update: 2009-05-27]

Occasionally I happen to see a piece of C++ code from an eager author that tries to utilize rvalue references but doesn’t understand them and gets it wrong. I remember that I had trouble fully grasping the rvalue reference concept. But now, it seems like that was way in the past. I can hardly remember why it was so difficult to understand rvalue references.

It may have something to do with the sources people use to learn about this feature. For example I wouldn’t consider the C++0x wikipedia article to be a good introduction for rvalue references. It seems too short for that. But this wikipedia article is a nice overview of what kind of features can be expected.

Let me just mention the real sources worth reading:

  • N1377: Move Proposal (2002)
  • N1385: Perfect Forwarding Problem (2002)
  • N2027: A Brief Introduction to Rvalue References (2006)
  • N2812: A Safety Problem with Rvalue References (2008)
  • N2844: Fixing a Safety Problem with Rvalue References (Revision 1, 2009)
  • Rvalue References 101by Dave Abrahams

I would recommend N2027 as an introduction and Dave Abraham’s article for more practical details including exception safety and return value optimizations.

Read the rest of this entry »

Written by pizer

April 13, 2009 at 12:50 pm

A little C++0x finger exercise

with 4 comments

Since the C++ compiler of GCC 4.3.2 comes with partial C++0x support and I had not tested all of these features yet, I decided to do a little finger exercise: “function”.

“function” is a useful library feature of Boost, TR1, and the upcoming C++ standard library. It’s basically a class template mimicing the behaviour of a function pointer. But it’s much more flexible because it applies to any “callable object” — function pointers and objects with overloaded function call operator. Also, the signatures don’t have to match perfectly as long as the parameters and return values are implicitly convertible. I’m going to show you how this library feature can be implemented with C++0x features — including variadic templates and rvalue references. But before going into any details let me give an example of how code that uses this “function” library might look like. In case you’re already familiar with this library feature you can skip the following piece of code and paragraph of text.

Read the rest of this entry »

Written by pizer

March 28, 2009 at 9:59 pm

Surprizing C++ reference-binding rules

with one comment

Isn’t it amazing? You see a very simple piece of code that looks perfectly fine and wonder why it is supposed to be ill-formed:

  struct foo {
    foo(const foo& rhs);

  void func1(const foo&);

  void func2() {
    foo o;
    func1( o ); // ok
    func1( foo() );  // ill-formed, foo's copy-ctor is private

Although an rvalue is allowed to bind to a reference-to-const, the C++ standard allows a compiler to copy an rvalue to another temporary rvalue before binding it to a reference-to-const. Obviously, you need to have a public copy constructor for that.

I really fail to see the merit in this rule. It seems that every available C++ compiler does not create an extra temporary object here and some of these compilers don’t even bother to report an error in this situation (g++ version 3.4.5 accepts the above code).

Fortunately, the upcoming version of the C++ standard removes the CopyConstructible requirement for reference binding. It makes the code above well-formed.

Source: this usenet thread

– P

Written by pizer

March 25, 2009 at 1:56 pm