is C++ dynamically or statically typed language or both?

P

puzzlecracker

I have seen these terms used in Gang of 4 but could never thoroughly
understand what it meant I perceive that C++ is both, but apparently
it is only a latter.

Can someone explain it?

thx
 
L

Luke Meyers

Well, you need to have clear definitions of both terms before you can
evaluate to what extent they do or do not apply to C++. Others may
correct me, but I'll rough out some definitions here:

Statically typed language -- a language which requires compile-time
enforcement of a type system.

Dynamically typed language -- a language which provides information
about types at runtime, and may permit operations based on that
information.

By these definitions, C++ is both. However, there is room for a lot of
gray here, and it has a great deal to do with programming practice, not
just the language standard. I'll use Java as a point of comparison.
Both C++ and Java have static type checking. However, C++'s static
typing system is much stronger, especially w.r.t. templates. C++
provides runtime facilities for typing as well (RTTI, typeid,
dynamic_cast), but these are language/library features which one can do
entirely without, often with good justification. C++ generally abides
by the philosophy that if you can perform the necessary determinations
statically (i.e. at compile time), you're better off doing that than
paying the runtime cost of determining them dynamically. By contrast,
in Java you pretty much agree up front to pay the cost for dynamic
typing, so it finds its way into standard Java idioms pretty
extensively. It's handy to have this available in Java, because of the
diminished static typing as compared to C++.

Does this help?

Luke
 
G

Gianni Mariani

puzzlecracker said:
I have seen these terms used in Gang of 4 but could never thoroughly
understand what it meant I perceive that C++ is both, but apparently
it is only a latter.

Can someone explain it?

C++ is strictly a statically typed language, however, you can create
classes that do dynamic typing, but from a C++ standpoint, this is an
application implementation detail, not somthing provided as a standard
language facility.
 
P

puzzlecracker

Luke said:
Well, you need to have clear definitions of both terms before you can
evaluate to what extent they do or do not apply to C++. Others may
correct me, but I'll rough out some definitions here:

Statically typed language -- a language which requires compile-time
enforcement of a type system.

Dynamically typed language -- a language which provides information
about types at runtime, and may permit operations based on that
information.

By these definitions, C++ is both. However, there is room for a lot of
gray here, and it has a great deal to do with programming practice, not
just the language standard. I'll use Java as a point of comparison.
Both C++ and Java have static type checking. However, C++'s static
typing system is much stronger, especially w.r.t. templates. C++
provides runtime facilities for typing as well (RTTI, typeid,
dynamic_cast), but these are language/library features which one can do
entirely without, often with good justification. C++ generally abides
by the philosophy that if you can perform the necessary determinations
statically (i.e. at compile time), you're better off doing that than
paying the runtime cost of determining them dynamically. By contrast,
in Java you pretty much agree up front to pay the cost for dynamic
typing, so it finds its way into standard Java idioms pretty
extensively. It's handy to have this available in Java, because of the
diminished static typing as compared to C++.

Does this help?

Luke

Thanks Luke. Indeed helpful!
However, C++'s static typing system is much stronger, especially w.r.t. templates

I am sure you missed the introduction of templates in jdk 5.0 (1.5).
requires compile-time enforcement of a type system.
How does c++ enforce that?


I thought run-time polymorphism, that is, through virtual functions,
make language of a dynamic type.
 
L

Luke Meyers

puzzlecracker said:
I am sure you missed the introduction of templates in jdk 5.0 (1.5).

I'm sure I didn't, though you should be more careful than to refer to
Java generics as "templates." They're superficially similar, but
fundamentally different. A specialization of a generic does not really
create a new type, unlike a template specialization. Generics also
don't support specializing on constants, like templates do (e.g.
template <int x> identity() { return x; }). The types are erased after
compilation, which is pretty limiting. You can't overload on different
specializations of a generic type, because the types are erased after
compilation, meaning that at runtime all specializations revert to be
the same type as if you had never specialized them. Generics support
the "container of T" paradigm well, but for templates that's only the
barest beginning.

To get back to C++, and your question about run-time polymorphism and
virtual functions: remember that the only thing that makes a virtual
function different is their dispatch -- that is, when I have this code:
Parent * p = new Child();
p->doStuff();
The call to doStuff is dispatched to the Child implementation of that
function, if Parent declared doStuff as virtual. This is accomplished
by means of a virtual function table, or "vtable." The vtable is just
a lookup table, computed at compilation time. I'm not up on the
details of how the dispatch is accomplished, but you can look that up
on your own if you're interested.

A bit more material on static and dynamic typing:
http://en.wikipedia.org/wiki/Data_type#Static_and_dynamic_typing
Reading this, I find that my original definitions could use some
improvement. But most of what I've said about C++ still stands. The
gist of it is that in a dynamically-typed language, the type of a
variable may depend on the execution path on which it occurs. For
example:
if (cond) {
x = 5;
} else {
x = "foo";
}
To do something like this in C++, you'd have to write appropriate
conversion operators, assignment operators, or constructors, and you
still wouldn't be changing the type of x, just setting the value of x
based on a conversion from some other type.

Anyway, the Wikipedia article knows more than me, so you should
probably just read that.

Luke
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top