Thread: Can I export this?

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    Can I export this?

    I'm trying to export this (unfinished) template class in a DLL and it isn't working. No complaints from the compiler / linker, it's not in the list in depends (I haven't tried to import it yet, which I'm going to do dynamically).

    Code:
    template <typename T> class EXPORTED CArray
    {
    public:
      CArray( size_t uSize ) : m_uSize( uSize )
      {
        // TODO: Attempt to allocate m_pData. Awaiting Debug library (Assert!)
      }
    
      ~CArray( void )
      {
        // TODO: Free m_pData
      }
    
      // Random access
      const T &operator[]( size_t uIndex ) const
      {
        // TODO: Assert validity of uIndex
        return m_pData[uIndex];
      }
    
      T &operator[]( size_t uIndex )
      {
        // TODO: As above
        return m_pData[uIndex];
      }
    
      const size_t &Size( void ) const
      {
        return m_uSize;
      }
    
    private:
      T       *m_pData;
      size_t  m_uSize;
    
    };
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Are you using the Comeau compiler or the Intel 7.x series? Because export is only supported by these two. And on the case of the Intel ones, I reckon it's off by default.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Sorry for forgetting this: MSVC 7.1 (VS .NET 2003).

    Is there any way around this short of a slight redesign?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I'm not versed in the template separation model. Others will certainly be able to help. However whatever little readings I did on the subject have taught me that if you can mimic export without the export keyword, you are really not using export. In other words, the separate compilation model is only possible with the export keyword. Also, that export is extremely hard to use correctly and is probably best judged on a case-by-case basis.

    If no one here will be able to help you any more than I did (I highly doubt it I can imagine at least 4 people who for sure know a lot about export), I suggest you get a book I bought a few months ago and that does have some coverage on the export feature of C++, Herb Sutter's Exceptional C++ Style 40 New Engineering Puzzles.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    You're trying to put a template class in a DLL? Is that even possible?

    A template doesn't define a class, it defines a way for the compiler to generate classes. E.g. std::vector<int> is a separate class from std::vector<char> or std::vector<float>. A template defines an infinite number of classes, which have analogous functions but each version of the same function will be very different in machine code.

    A given instance of the template is only created when you use it in your code, and so the template can't be compiled unless you actually create an object based on that template.

    I mean, the machine code generated for a function of vector<int> isn't the same as the machine code generated for the analogous function of vector<char>. Maybe I'm misunderstanding what you're trying to do.

    In general if you want to make a template library, you'll do what STL does which is put all the template code inside header files and just #include them. You can't compile the template code as a DLL then link it, unless you want to restrict the templates to only specific classes.

    For example, you could make and export the CArray<int> class from a DLL, but that wouldn't help you at all if you wanted to use CArray<double> or CArray<float> or CArray<UserDefinedClass>. There's a reason template classes and functions are generated only as needed -- there's an infinite number of possible classes that a single template could create.
    Last edited by Cat; 11-08-2006 at 12:56 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Even the template separation model cannot make templates in DLLs work. In static libs, yeah, but not dynamics. (The DLL loader would have to instantiate templates.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    OK guys thanks. Admitedly I've still got a lot to learn about templates (and C++ in general) and I'll definately get that book (got to do something with my student loan!).
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dll Export table
    By P4R4N01D in forum Windows Programming
    Replies: 7
    Last Post: 04-08-2008, 09:41 PM
  2. run windows exe
    By munna_dude in forum Linux Programming
    Replies: 3
    Last Post: 10-10-2007, 01:12 AM
  3. Replies: 3
    Last Post: 01-14-2007, 11:20 AM
  4. Datagrid Export to MS Excel with C++ .NET
    By Gerudom in forum Windows Programming
    Replies: 3
    Last Post: 06-09-2006, 07:58 AM
  5. How to export dll function for VB
    By joeyzt in forum C++ Programming
    Replies: 0
    Last Post: 01-31-2005, 12:33 PM