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

Discussion in 'C++' started by puzzlecracker, Dec 25, 2005.

  1. 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
     
    puzzlecracker, Dec 25, 2005
    #1
    1. Advertising

  2. puzzlecracker

    Luke Meyers Guest

    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
     
    Luke Meyers, Dec 25, 2005
    #2
    1. Advertising

  3. 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.
     
    Gianni Mariani, Dec 25, 2005
    #3
  4. 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.
     
    puzzlecracker, Dec 25, 2005
    #4
  5. puzzlecracker

    Luke Meyers Guest

    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_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
     
    Luke Meyers, Dec 25, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ersin Gençtürk
    Replies:
    1
    Views:
    3,557
    Ersin Gençtürk
    Oct 6, 2004
  2. puzzlecracker
    Replies:
    9
    Views:
    1,777
    puzzlecracker
    Dec 30, 2005
  3. Ville Vainio
    Replies:
    2
    Views:
    369
    Shawn Wheatley
    Jul 16, 2004
  4. Yingjie Lan
    Replies:
    4
    Views:
    343
    John Nagle
    Jan 29, 2010
  5. Enrique Meza
    Replies:
    0
    Views:
    138
    Enrique Meza
    Jul 24, 2003
Loading...

Share This Page