[cfe-dev] Function Instantiation: Where? Or is it?

Larisse Voufo lvoufo at cs.indiana.edu
Mon Jan 31 13:43:54 PST 2011


Thanks. That was a lot helpful. However, I do have another question.
Consider the following two case scenari:

Case 1:
------------
The previous one, with
(1) f() defined as a concept member, and
(2) func() a restricted template


Case 2:
------------
(1) f() is a global function, and
(2) func() is a simple template, as in

void f() { }

template<typename T>
void func(T a) {
    f(0);
}

int main(int argc, char **argv)
{
    int i=0;
    func<int>(i);
}


Observation:
----------------------
In both cases, f() never seems to be "marked for instantiation", at least
not through Sema::
MarkDeclarationReferenced. However, f() does seem to eventually get
instantiated in the "Case 2" somehow, whereas it never does in "Case 1". Any
idea?

1) When does f() get instantiated in "Case 2"?
2) is this connected to lookup somehow?
     I have made some very minor changes in Sema::CppLookupName(), and I'm
not sure how this could affect instantiation...

Thanks,

-- Larisse.





On Mon, Jan 31, 2011 at 11:42 AM, Douglas Gregor <dgregor at apple.com> wrote:

>
> On Jan 31, 2011, at 8:12 AM, Larisse Voufo wrote:
>
>
> Folks --
>
> This has had me puzzled for a while, and I could use an answer asap.
> I am implementing concepts in Clang and seem to be missing a crucial piece
> of understanding, either of C++, or of Clang.
> Basically, I have gotten mostly everything else implemented and working
> fine to the extend that I want them to, except for concept members
> instantiation (?).
>
> Basic Example:  instantiation.cpp
> ----------------------
>
> concept B<typename T> {
>   void f(int) { }
> }
>
> concept_map B<int> {
>   void f(int) { }
> }
>
> template<typename T>
> requires (B<T>)
> void func(T a) {
> //    f(a);
>     f(0);
> }
>
> int main(int argc, char **argv)
> {
>     int i=0;
>     func<int>(i);
> }
>
> Compilation:  clang++ instantiation.cpp -o example
> ----------------------
> It seems to be failing at link time, with the following error message:
>
> Undefined symbols:
>   "__ZN1B1fEi", referenced from:
>       __Z4funcIiEvT_ in cc-MXLAc6.o
> ld: symbol(s) not found
>
>
> Any idea? Anyone?
> 1) At which point are functions marked for instantiation?
>
>
> Look for calls to Sema::MarkDeclarationReferenced throughout Sema.
>
> 2 ) At which point are they instantiated ?
>
>
> At the end of the translation unit.
>
> 3) If instantiated, why would they not be visible by the linker?
>
>
> Probably because the template instantiation mechanism couldn't find the
> definition of B<int>::f(int).
>
> - Doug
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20110131/2af6893a/attachment.html>


More information about the cfe-dev mailing list