Preventing Evil Operator Overloading

While doing some research on allocators, I noticed that the language specification has some interesting wording with regards to getting the address of an element from an allocator. Specifically, it says (Section 20.6.9.1 Clauses 2 & 3):

Returns: The actual address of the object referenced by x, even in the presence of an overloaded operator&.

How do you do that?
Continue reading

Posted in C/C++ | Tagged | Leave a comment

Contextual Keywords

I believe the C++ standards committee got some things wrong in the distant past. Converting constructors work implicitly with an assignment operation, function hiding and overriding are not explicit, there’s no way to prevent a subclass from providing further overrides to virtual functions, etc. But with the new C++11, I believe they are trying to make amends. I want to discuss some of the new contextual keywords in the latest version of C++.
Continue reading

Posted in C/C++ | Tagged , , | 2 Comments

Understanding Attributes

The new C++11 standard includes the ability to specify “attributes” for various declarations. The concept of attributes will be familiar to you if you’ve done work in languages like C# or Java. However, there are major differences between C++ attributes and attributes from other languages. This blog posting will cover what this new feature for C++ means for everyday programmers.
Continue reading

Posted in C/C++ | Tagged , , | Leave a comment

When to be Explicit

In C++, a constructor that accepts a single parameter non-defaulted parameter is also considered a converting constructor. Converting constructors allow you to initialize a class instance using that single parameter type either via explicit construction, or via an assignment construction. For instance:

class Foo {
public:
	Foo( int i );
	Foo( const char *s, bool b = false );
};

Foo f = 12;  // Calls Foo( int )
Foo f2 = "Aaron";  // Calls Foo( const char *, bool )

This is a handy feature for some class types, but can be entirely incorrect for other class types. The purpose of this post is to explore the problems of assignment construction and how to overcome them.
Continue reading

Posted in C/C++ | Tagged , | 2 Comments

What I Learned Today About Virtual Functions

What is wrong with the following class declarations?

class Base {
public:
	virtual void Foo();	
	virtual void Bar();
};

class Derived : public Base {
public:
	virtual void Foo();
	void Bar();
};

It turns out that the correct definition is the one to Bar, which came as a surprise to me.
Continue reading

Posted in C/C++ | Tagged | 2 Comments

Static Polymorphism in C++

One of my coworkers recently asked me to help him solve a problem he was having in code. He had a base class with several derived classes, and he wanted to add a static method to the base class, but have the derived classes determine the implementation. Basically, he wanted virtual method dispatch semantics, while still using static method dispatching. Obviously he couldn’t just declare a static virtual function!
Continue reading

Posted in C/C++ | Tagged , | 23 Comments

What Happens When You Load a Library

At this point in time, I think it’s safe to say that almost all programmers on Windows take shared libraries (DLLs) for granted. They’re this background thing that always “just works” (even if you do recall the ‘DLL hell’ days). They’re the reason .NET’s side-by-side assemblies work. They’re the main force in localization these days. All of the OS APIs you use are exposed from them. They’re ubiquitous. And very few people understand how they work under the hood.
Continue reading

Posted in Win32 | Tagged | Leave a comment

Text Encodings for Cross-Platform Frameworks

When creating cross-platform frameworks, text encodings can be a hairy topic. There are multiple different encodings to choose from as well as edge cases to be concerned about. This post is going to cover some suggestions on how to handle string encodings, but keep in mind that there’s more than one way to skin a cat. This post assumes you have at least a rudimentary understanding of what text encodings are and how they work.
Continue reading

Posted in Framework Design | Tagged , , | 1 Comment

Crashing: Easy to Do When You Don’t Want to, Hard When You Do

The challenge: in C or C++, come up with a way to crash your application, running as little code as possible. It should be a cross-platform solution that works with any compiler, on any system, with any CPU architecture.
Continue reading

Posted in C/C++ | Tagged , | 4 Comments

List Initialization

One of the new features in C++0x has been to make a consistent mechanism for initialization via a list. In previous versions of C++, it was inconsistent how you would initialize lists which would lead to a small amount of confusion. For instance:

int foo[] = { 1, 2, 3, 4 };  // OK
std::vector bar = { 1, 2, 3, 4 }; // Error
std::vector baz( 1, 2, 3, 4 );  // OK
int quux[]( 1, 2, 3, 4 );  // Error

Sometimes you could use list of values in braces, sometimes you couldn’t. Sometimes you could use a constructor, sometimes you couldn’t. C++0x has rectified this issue as part of the push towards more uniform initialization. Now you can use braced initializer lists everywhere.
Continue reading

Posted in C/C++ | Tagged , | 2 Comments