extern templates
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: extern templates

  1. #1
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    extern templates

    I am trying to use extern templates in VS 2010.

    In most cases, people use extern templates purely as an optimization, but in my case I actually need them for correctness; in the header, the type instantiating the template is forward declared, so I want to defer instantiation to the source file where I have a complete type. Otherwise I'll get a compilation error. (It's valid for the template itself to be incomplete in the header, since it's just a pointer.)

    Header:
    Code:
    class AbstractFolder;
    extern template class Watcher<AbstractFolder>;
    Source:
    Code:
    #include "abstractfolder.h"
    template class Watcher<AbstractFolder>;
    I've *almost* got it working, but I've run into this:
    error C2960: 'Watcher<T>' : inconsistent explicit instantiations, a previous explicit instantiation specified 'extern template'

    This seems like one of the stupidest errors ever, since my intent is clearly to override the "extern" and force instantiation at that point, but the compiler doesn't seem to want to let me do that.

    Unfortunately it's impossible to do this in a source file where the header is not included, since the header with the extern declaration is included from abstractfolder.h (which is the reason forward declarations are needed in the first place).

    Two questions. First, is it possible to make VS2010 do what I want here? Second, we may be upgrading to VS2013 soon; will it give me the behavior I want?

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: extern templates

    Oh, and the strange thing is that if I look up C2960, I get an unrelated error from VS6. There doesn't appear to be any documentation of this error message.

  3. #3

    Re: extern templates

    Hey. Mind if you send me your files your coding. I can check to see if I get the same issue. I can try to fix it and send you the fixed code.
    thanks. Hope I can help.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,441

    Re: extern templates

    Zip up a solution of a small sample that repros the problem so someone can try it out in VS 2013.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center