This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [G++] templates not instantiated early enough


On 4 Jun 2004, Gabriel Dos Reis wrote:
> | Yes, of course you're right.  The problem is that the function prototype
> | is being mark_used and sent to the backend.  At this point the template is
> | not instantiated yet, so the backend can't look into the structure
> | argument to see what it contains.  Andrew pointed out an example of how
> | this can actually result in an erroneous program not being flagged as
> | such.
>
> Such as your program :-) -- you used foo but you did not provide a
> definition for it.  Note, that if you replace the template with an
> ordinary function, you get the same behaviour (the program is still
> ill-formed).

I don't think that a definition of foo is needed: it could be provided in
a different translation unit.

>      struct fu;
>      void foo(fu);     // OK
>      void bar() {
>         foo;           // use of foo, needs definition
>      }

Ah, excellent point!  Hrm, that makes things a bit more complex.  :)


> |  However, the foo function is
> | referenced in the program, despite the fact that it is not called.
>
> So, you need to define the function foo.  Now, it you *define* the
> function foo, then because some_template<int> is used as a parameter
> type, it needs to be instantiated.  But you were not doing that in the
> program fragment you showed.  Therefore the error, if any, was in your
> program.

The problem is that I need the information on the use as well as the
definition side of things.  If the definition is in a different xlation
unit, it doesn't help.  :(

I obviously need to take a step back and try to come up with other
solutions to the problem, as you've deftly pointed out that this has
nothing to do with C++: it can happen in C too.  Sorry for the confusion.
:)

Thanks for the help!

-Chris

-- 
http://llvm.cs.uiuc.edu/
http://www.nondot.org/~sabre/Projects/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]