Pizer’s Weblog

programming, DSP, math

Posts Tagged ‘C++

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();
  private:
    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

Books, Books, Books (on C++)

with 5 comments

I already said it once in another post but I think it deserves to be mentioned again: Don’t underestimate the usefulness of good books about C++! Prefer buying a good book over searching the internet for tutorials, examples, explanations if you want to learn the language and/or learn about useful idioms, guidelines, etc…

I recently added two other books to my personal library: “Effective C++” and “C++ Coding Standards”. It took me only 3 weeks to finish both books. That means they were interesting and insightful. :-)

Now, it’s these four C++ books that are in my posession:

  • The C++ Programming Language (Special Edition) by Bjarne Stroustrup
  • Modern C++ Design by Andrei Alexandrescu
  • Effective C++ (Third Edition) by Scott Meyers
  • C++ Coding Standards by Herb Sutter & Andrei Alexandrescu

I wouldn’t mention them here if I didn’t like them. :-)

- P

Written by pizer

February 22, 2009 at 2:01 am

Posted in Programming

Tagged with , , ,

Follow

Get every new post delivered to your Inbox.