Strong Typing or Naked Primitives

Update 1: This post has been updated as the result of comments by legalize. Deletions are indicated by strikethough and additions by text in blue.

Update 2: Added reference 6.

Is C++ Strongly or Weakly Typed?

There are a number of definitions of strong and weak typing. If you are interested, you can look them up using your favourite search engine. You can also see some of the references below. I am not going to add my definitions; I will just say that I think C++ is both strongly and weakly typed, and the programmer can do much to turn those weakly typed parts into strongly typed parts. That is the topic of this post.

Note: There is nothing earth shaking in this post. You will find a number of similar posts on the Internet, with the only differences being the examples. I have written this to help noobs, and to provide background information for future posts.

There is no var data type in C++ like there is in some languages, where the type is simply what appears most appropriate at that point in the code. C++ does have auto, but the type is determined at the time the variable is defined and cannot be implicitly changed. Types can be coerced or converted (cast) into other types (e.g. an integer into a floating point, an integer into a pointer, a double to a floating point number, and so forth). These coercions are explicit rather than implicit so theoretically this does not violate strong typing; it can cause problems, though.

Common Variable Types as Arguments

One place where problems occur is in the use of common variable types as arguments. This and the following two posts will look this problem and at potential solutions.

Look at the example, below.

Example (Weakly TypedInteger Arguments)

I have been creating a C++ library for Vulkan. One of the lower-level classes that I need is Size, a class that encapsulates the width and height of an object. So let’s look at the first iteration for this class (actually a struct):

struct Size final
{
public:
    Size(const uint32_t w, const uint32_t h) 
        : m_width(w), m_height(h) {}
    uint32_t getWidth() const noexcept 
        { return m_width; }
    uint32_t getHeight() const noexcept 
        { return m_height;}
private:
    uint32_t m_width;
    uint32_t m_height;
};

How would this be used? Like this:

Size size(400, 300);

So what is wrong with this? Look at this line of code in six months. Is 400 the width or the height?

The constructor takes two integers (uint32_t values) as input. That’s fine; everyone knows that width is specified before height, right? Well maybe in your world, but there is no such guarantee in mine. If by chance or mistake, the user of this struct specifies the height before the width, then that is just plain wrong. The program will compile, and the error may or may not be caught at runtime.

Example (Strongly TypedClasses as Arguments)

Let’s fix this. To do so, we have to change the argument types in the constructor to indicate that one is a width and the other is a height. Let’s use Width and Height as the argument types:

struct Size final
{
public:
    Size(const Width& w, const Height& h) 
        : m_width(w), m_height(h) {}
    Width getWidth() const noexcept 
        { return m_width; }
    Height getHeight() const noexcept 
        { return m_height;}
private:
    Width m_width;
    Height m_height;
};

and here are the definitions for Width and Height:

class Width
{
public:
	explicit Width(const uint32_t width) 
            : m_width(width) {}
	uint32_t getWidth() const noexcept 
            { return m_width; }
        operator uint32_t() { return m_width; }
private:
	uint32_t m_width;
};

class Height
{
public:
	explicit Height(const uint32_t height) 
            : m_height(height) {}
	uint32_t getHeight() const noexcept 
            { return m_height; }
        operator uint32_t() { return m_height; }
private:
	uint32_t m_height;
};

We create a Size object as follows:

Size size(Width(400), Height(300));

Now there is no confusion; the width is 400 units and Height is 300 units, whatever units is. If the programmer specifies Height before Width, the compiler will catch this and the code will not compile.

Note that, instead, I could add a second constructor to Size that takes Height and then Width as arguments. The program will then compile, and there will still be no confusion as to what the arguments represent.

Conclusions

  1. C++ is both strongly and weakly typed. Using the common variable types as arguments to functions and methods can still cause a number of problems.
  2. By creating classes for weakly typed values, it is possible to make them strongly typed. Replacing these arguments with classes helps both the compiler and the programmer to ensure that arguments to functions and methods are both correct and in the correct order.

References

  1. Strong and Weak Typing
  2. Is C Strongly Typed?
  3. Is C++ Considered Weakly Typed? Why?
  4. Use Stronger Types!
  5. C++ strongly typed typedef
  6. String types for strong interfaces
Advertisements