C++/CLI: make public for template type

2019-07-11 02:57发布

问题:

AFAIK #pragma make_public supports only native non-template types.

But, is there some other way to make template type as public?

回答1:

No. But read here for some workarounds:

  • Best workaround for compiler error C2158: make_public does not support native template types

  • http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/b43cca63-b0bf-451e-b8fe-74e9c618b8c4/



回答2:

Templates don't have external linkage. Not in native C++, not in C++/CLI either. You solve it the same way, put them in a .h header file and #include it in any source file where you want to use the template. They are of course not available to any other managed code that isn't written in C++/CLI.

Do favor the generic keyword in C++/CLI, generics in managed code do have the equivalent of external linkage.