Explicit instantiation/specialization confusion

Discussion in 'C++' started by Dilip, Aug 15, 2006.

  1. Dilip

    Dilip Guest

    I am stumbling my way through C++ templates and I keep running into way
    too many questions. Here is one:

    If a library writer exposes a function template like so:

    template<typename T>
    void some_func(T const& x)
    {
    }

    Lets say the client code explicitly instantiates it:

    template void some_func(int);

    Later if the library writer releases a new version that explicitly
    specializes this template for the same data type, what happens to the
    client code? The book I am reading says this issue is still pending
    with C++ committee. Has there been any decisions taken?

    Or am I asking the question backwards? maybe client code is never
    allowed to explicitly instantiate function/class templates from a
    library?
     
    Dilip, Aug 15, 2006
    #1
    1. Advertising

  2. Dilip wrote:
    > I am stumbling my way through C++ templates and I keep running into
    > way too many questions. Here is one:
    >
    > If a library writer exposes a function template like so:
    >
    > template<typename T>
    > void some_func(T const& x)
    > {
    > }
    >
    > Lets say the client code explicitly instantiates it:
    >
    > template void some_func(int);


    I don't think it's a match. What's "T" in this case?

    > Later if the library writer releases a new version that explicitly
    > specializes this template for the same data type, what happens to the
    > client code? The book I am reading says this issue is still pending
    > with C++ committee. Has there been any decisions taken?


    You need to ask in 'comp.std.c++' about any Standard issues.

    > Or am I asking the question backwards? maybe client code is never
    > allowed to explicitly instantiate function/class templates from a
    > library?


    The explicit instantiation makes sure that the code is created. Where
    it comes from is the user's responsibility. Generally, if the instantiation
    appears in the header, all TUs that include that header participate equally
    in the contest to hold the actual generated code, and the linker possesses
    the ablity to resolve that. Also, all instantiations have to be the same,
    otherwise ODR is violated, and I don't know if nowadays the linkers actually
    care to compare the instantiations. Usually the first one is picked. Which
    of the several becomes "the first one" is undefined.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 15, 2006
    #2
    1. Advertising

  3. Dilip

    Dilip Guest

    Victor Bazarov wrote:
    > Dilip wrote:
    > > I am stumbling my way through C++ templates and I keep running into
    > > way too many questions. Here is one:
    > >
    > > If a library writer exposes a function template like so:
    > >
    > > template<typename T>
    > > void some_func(T const& x)
    > > {
    > > }
    > >
    > > Lets say the client code explicitly instantiates it:
    > >
    > > template void some_func(int);

    >
    > I don't think it's a match. What's "T" in this case?


    isnt' it 'int'?

    > > Or am I asking the question backwards? maybe client code is never
    > > allowed to explicitly instantiate function/class templates from a
    > > library?

    >
    > The explicit instantiation makes sure that the code is created. Where
    > it comes from is the user's responsibility.


    Understood. I think I didn't phrase my question properly. Assume for
    the function template above, I have the following instantiation in one
    of my translation units in the client code:

    template void some_func(some_cool_class);

    Some days later the library writer releases a new version that includes
    explicit specialization for the same function template with the same
    some_cool_class by some nasty quirk of fate:

    template<>
    void some_func(some_cool_class const& x)
    {
    }

    Now if I link with this new version, am I in boom-land?
     
    Dilip, Aug 16, 2006
    #3
  4. Dilip wrote:
    > Victor Bazarov wrote:
    >> Dilip wrote:
    >>> I am stumbling my way through C++ templates and I keep running into
    >>> way too many questions. Here is one:
    >>>
    >>> If a library writer exposes a function template like so:
    >>>
    >>> template<typename T>
    >>> void some_func(T const& x)
    >>> {
    >>> }
    >>>
    >>> Lets say the client code explicitly instantiates it:
    >>>
    >>> template void some_func(int);

    >>
    >> I don't think it's a match. What's "T" in this case?

    >
    > isnt' it 'int'?


    No. The argument is a *reference* to const T. What in 'int' is a ref
    to what? Nothing. That's why T cannot be figured out.

    >>> Or am I asking the question backwards? maybe client code is never
    >>> allowed to explicitly instantiate function/class templates from a
    >>> library?

    >>
    >> The explicit instantiation makes sure that the code is created.
    >> Where it comes from is the user's responsibility.

    >
    > Understood. I think I didn't phrase my question properly. Assume for
    > the function template above, I have the following instantiation in one
    > of my translation units in the client code:
    >
    > template void some_func(some_cool_class);
    >
    > Some days later the library writer releases a new version that
    > includes explicit specialization for the same function template with
    > the same some_cool_class by some nasty quirk of fate:
    >
    > template<>
    > void some_func(some_cool_class const& x)
    > {
    > }
    >
    > Now if I link with this new version, am I in boom-land?


    Unknown. Probably. You might want to carefully read the "release notes"
    of that library to know what's changed.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 16, 2006
    #4
    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. Rolf Magnus
    Replies:
    0
    Views:
    884
    Rolf Magnus
    Jul 23, 2003
  2. C. Carbonera
    Replies:
    4
    Views:
    1,387
    C. Carbonera
    Feb 5, 2004
  3. Fernando Cuenca
    Replies:
    4
    Views:
    2,560
    Gianni Mariani
    Sep 6, 2004
  4. Andy
    Replies:
    5
    Views:
    527
    Shezan Baig
    Jan 30, 2005
  5. Replies:
    1
    Views:
    619
    Salt_Peter
    Dec 25, 2006
Loading...

Share This Page