Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > is C++ dynamically or statically typed language or both?

Reply
Thread Tools

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

 
 
puzzlecracker
Guest
Posts: n/a
 
      12-25-2005
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

 
Reply With Quote
 
 
 
 
Luke Meyers
Guest
Posts: n/a
 
      12-25-2005
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

 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      12-25-2005
puzzlecracker wrote:
> 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.
 
Reply With Quote
 
puzzlecracker
Guest
Posts: n/a
 
      12-25-2005

Luke Meyers wrote:
> 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.

 
Reply With Quote
 
Luke Meyers
Guest
Posts: n/a
 
      12-25-2005
puzzlecracker wrote:
> 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_ty...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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
mix statically typed with dynamically typed Yingjie Lan Python 4 01-29-2010 08:50 AM
is Java dynamically or statically typed language or both? puzzlecracker Java 9 12-30-2005 08:26 PM
How can I add a row from a typed datatable to another instance of that typed datatable? Ersin Gençtürk ASP .Net 1 10-06-2004 01:11 PM
Boo: Statically typed Python with type inference is done (well, kinda ;-) Ville Vainio Python 2 07-16-2004 05:21 PM
"The VMs are designed for statically typed languages..."... Enrique Meza Ruby 0 07-24-2003 09:49 PM



Advertisments