سؤال

I need to export template specialization.

Are the following definitions equal?

typedef std::vector<int> MY_API MyIntArray; // (1)

template class MY_API std::vector<int>; // (2)
typedef std::vector<int> MyIntArray;

Common MY_API macro definition looks like #define MY_API __declspec(dllexport).

And another question is -- does template specialization occur at typedef std::vector<int> MyIntArray;?


I've alreade seen a bunch of a question about using STL classes across modules/dll boundaries and potential issues.

هل كانت مفيدة؟

المحلول

No -- the typedef is a slightly (but only slightly) smarter version of a #define, that equates the specified name with the specified type. It adds an entry in the compiler's symbol table saying this name stands for that type.

A typedef does nothing more than that though. It specifically does not instantiate the specified type, so it is not equivalent to an explicit instantiation of the template in question. (As to why it explicitly doesn't instantiate the type: for one, because you can create a typedef of an incomplete type, which can't be instantiated until the type is completed).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top