Templates - Garbage In Garbage Not Out

Discussion in 'C++' started by ramiro_b@yahoo.com, Jul 25, 2005.

  1. Guest

    All,

    In a .h file, I am declaring/defining a template as

    template<class T> class MyClass
    {
    public:
    void test()
    {
    haskdfhsjfksfhkfskfsfkklkll89829482498020
    gdgk;ek;kd;gkdkd;gdkg;d
    }
    };

    and for some reason, it is compiling fine, even with the 'garbage' in
    the function body. Somehow it doesn't see my logic in the body of the
    function in compile time. Why? Any help? I am using CC 5.3 (Forte
    Developer 6.0 under Solaris 8).

    Thanks!

    -RB
    , Jul 25, 2005
    #1
    1. Advertising

  2. Since your class is templatized, only a first-pass parse is performed.
    The first-pass is simply a syntactic check, not a semantic one. Since
    your "garbage" line technically consists of well-formed syntactic
    tokens, the compiler doesn't complain.

    If, however, you try to instantiate MyClass (ie: MyClass<SomeClass> t;
    ), the compiler will complain. This happens because when MyClass is
    instantiated with a particular class T, the compiler *will*
    semantically process the tokens in test().

    --Steve
    mrstephengross, Jul 25, 2005
    #2
    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. Fred
    Replies:
    1
    Views:
    597
    Neredbojias
    Sep 26, 2005
  2. John Harrison

    using templates in templates

    John Harrison, Jul 31, 2003, in forum: C++
    Replies:
    8
    Views:
    380
    Torsten Curdt
    Jul 31, 2003
  3. JKop
    Replies:
    3
    Views:
    468
  4. recover
    Replies:
    2
    Views:
    801
    recover
    Jul 25, 2006
  5. rickhg12hs
    Replies:
    5
    Views:
    136
    rickhg12hs
    Apr 15, 2006
Loading...

Share This Page