C++ strong types

Hi all,

I encountered such a term "C++ strong type" and have no clue what it is. I also surfed the Web but the resources explaining it are either too advanced or too detailed.

Will you say what a cpp strong type is, in simple language, and demonstrate it with an example, please?

I've heard that it's very important in embedded programming.
I guess this is about strongly-typed/strong typing.

This basically means variables know their type at compile time and it wont change.

For example, you're not obliged to strong type with python, so you can write
1
2
def fun(a, b):
    return a + b


And you can give whatever you want to this function, but in C++, you have to do one function per type or to template it (which is going to create at compile time a version of the function for each type used with it in your code).
Last edited on
There's two dimensions when it comes to typing in a language.
- First one is the weak vs. strong dimension.
- Second one is the static vs. dynamic dimension.

Weak vs. strong, in my understanding, is how specific a type needs to be, and how easily it can be implicitly converted or used as another type. For example, a string is different than an int. If you have a function that takes in a string, you can't pass it an int. On the other hand, a double also is not an int, but if you pass an int into a function expecting a double, it will be implicitly converted into a double, so this is "weaker" in a sense. A void* (void pointer) is about the weakest type in C++, because it can point to basically any other type of object.
error: could not convert 'a' from 'int' to 'std::string {aka std::basic_string<char>}'


Static vs. dynamic is about whether or not types are known at compile-time.

C++ is a static language, because all objects have a particular type at compile-time. You can't change the same object from a Foo to a Bar at run-time. However, parts of C++ have weak typing, because when you see an object of type void*, you don't actually know what the object it points to is.
Last edited on
Topic archived. No new replies allowed.