Is C ++ considered weakly typed? What for?

I have always believed that C ++ is one of the most strongly typed languages. Therefore, I was very shocked that Table 3 of this article claims that C ++ is weakly typed.

Obviously

C and C ++ are considered weakly typed, because of the casting of types, you can interpret the structure field, which was an integer as a pointer.

Is the existence of a different type of casting all that matters? Is the clear clarity of such garbage unimportant?

In general, is it generally accepted that C ++ is weakly typed? Why?

+15
source share
5 answers

This article first says:

On the contrary, a language is weakly typed if type confusion can occur silently (undetected) and ultimately cause errors that are difficult to localize.

And then states:

In addition, C and C ++ are considered weakly typed, because of the casting of types, you can interpret the structure field, which was an integer as a pointer.

This seems to be a contradiction. In C and C ++, the type confusion that may result from a cast will not occur silently - there is a cast! This does not show that any of these languages ​​is weakly typed, at least not by definition in this article.

Thus, by definition in the document, C and C ++ can still be considered weakly typed. As already noted in the comments on this issue, cases where the language supports implicit type conversions. Many types can be implicitly converted to bool , a literal zero of type int can be seamlessly converted to any type of pointer, there are conversions between integers of different sizes, etc. Therefore, this seems like a good reason to consider C and C ++ is weakly typed for goals of the article.

For C (but not C ++) there are also more dangerous implicit conversions that are worth mentioning:

 int main() { int i = 0; void *v = &i; char *c = v; return *c; } 

For the purposes of this article, this must necessarily be considered weakly typed. The bits are reinterpreted quietly and can be made much worse by changing them to use completely unrelated types that have silent undefined behavior, which usually has the same effect as reinterpreting bits, but explodes in mysterious but sometimes funny ways when optimizing .

In general, however, I think that there is no fixed definition of "strongly typed" and "weakly typed." There are various classes, a language that is strongly typed compared to assembly can be weakly typed compared to Pascal. To determine if C or C ++ is weakly typed, you first need to ask what you want weakly typed.

+25
source

"weakly typed" is a rather subjective term. I prefer the terms “strongly typed” and “statically typed” versus “weakly typed” and “dynamically typed” , because they are more objective and more accurate words.

From what I can say, people usually use "weakly typed" as a derogatory term, which means "I don't like the concept of types in this language." This is a kind of argument ad hominem (or rather, argumentum ad linguam) for those who cannot raise professional or technical arguments against a particular language.

The term "strongly typed" also has slightly different interpretations; The generally accepted meaning, in my experience, is that "the compiler generates errors if the types do not match." Another interpretation is that there is “no or several implicit conversions”. Based on this, C ++ can indeed be considered a strongly typed language, and most often it is considered such. I would say that the general consensus on C ++ is that it strongly typed language.

Of course, we could try a finer approach to the issue and say that parts of the language are strongly typed (this is the majority of cases), other parts are weakly typed (several implicit conversions, for example, arithmetic conversions and four types of explicit conversions).

In addition, there are some programmers, especially beginners, who are not familiar with more than several languages, who do not intend or cannot distinguish between "strict" and "static", dynamically, and combine two - otherwise orthogonal - concepts based on their limited experience (as a rule, the ratio of dynamism and free printing in popular scripting languages, for example).

In fact, parts of C ++ (virtual calls) impose a requirement on a type system to be partially dynamic, but other things in the standard require it to be strict. Again, this is not a problem, as these are orthogonal concepts.

To summarize: probably the language does not fit completely, completely into one category or another, but we can say what special property of this language dominates. In C ++, strict certainty strictly dominates.

+8
source

On the contrary, a language is weakly typed if type confusion can occur silently (undetected) and ultimately cause errors that are difficult to localize.

Well, this can happen in C ++, for example:

 #define _USE_MATH_DEFINES #include <iostream> #include <cmath> #include <limits> void f(char n) { std::cout << "f(char)\n"; } void f(int n) { std::cout << "f(int)\n"; } void g(int n) { std::cout << "f(int)\n"; } int main() { float fl = M_PI; // silent conversion to float may lose precision f(8 + '0'); // potentially unintended treatment as int unsigned n = std::numeric_limits<unsigned>::max(); g(n); // potentially unintended treatment as int } 

In addition, C and C ++ are considered weakly typed, because of the casting of types, you can interpret the structure field, which was an integer as a pointer.

Ummmm ... not through any implicit conversion, so it's a dumb argument. C ++ allows explicit casting between types, but this is hardly "weak" - it does not happen by accident / quietly, as the definition of your own site above requires.

Is the existence of a different type of casting all that matters? Is the obvious need for such garbage unimportant?

The explanation is an important consideration IMHO. Providing the programmer with a redefinition of knowledge about types of compilers is one of the “powerful” features of C ++, and not some weakness. It is not subject to accidental use.

In general, is it generally accepted that C ++ is weakly typed? Why?

No - I do not think this is accepted. C ++ is fairly strongly typed, and the ways in which it was lenient, which historically caused problems, were thrown back, such as implicit casts from void* to other types of pointers and finer control using explicit statements and constructor statements.

+2
source

So, since the creator of C ++, Bjarn Straustrup, in the "C ++ Programming Language" (4th edition) says that the language is strongly typed, I would take his word:

C ++ programming is based on strict static type checking , and most methods are aimed at achieving a high level of abstraction and directly presenting the ideas of programmers. This can usually be done without sacrificing lead time and space efficiency compared to lower-level methods. To take advantage of C ++, programmers coming to it from another language must learn and learn the idiomatic C ++ style and programming technique. The same applies to programmers used in earlier and less expressive versions of C ++.

In this 1994 video lecture, he also states that the weak C type system really bothered him, and so he made C ++ strictly typed: C ++ Design, Bjarne Straustrup's lecture

+1
source

Let me give you a simple example:

  if ( a + b ) 

C / C + = allows implicit conversion from float to int to Boolean.

A strongly typed language does not allow such an implicit conversion.

-4
source

Source: https://habr.com/ru/post/977752/


All Articles