8

Consider the following module:

module M;

// a private, non-exporting function
int id(int x) {
    return x;
}

export
template <class T>
int f(T x) {
    return id(0);
}

export
int g(int y) {
    return id(1);
}

And the following C++ code using it:

import M;

int main() { 
    g(42);
    return 0; 
}

It successfully compiles with VS2015 update 1 and works, but if I replace g with f, the compiler complains: error C3861: 'id': identifier not found.

How to fix it?

Ignat Loskutov
  • 781
  • 4
  • 9
  • Are you compling with the correct flags for modules? I think it won't work from the IDE itself – paulm Mar 03 '16 at 13:47

1 Answers1

3

You face this problem because of templates instantiation rules. For the same reason as you include templates definition in C++ header files (and don't define them in separate .cpp files), you can't export template function from module in this way.

It's not a good practice to export template functions or classes from module because you should have all instantiations, that will probably be used, within this module. However if you want to implement it in this way for some reason, you should instantiate function f() with T as int in the module, e.g. add useless call with integer argument within this module.

CodeFuller
  • 30,317
  • 3
  • 63
  • 79
  • The reason of such behaviour is pretty clear, what’s unclear is how to export template functions then. Do I have to use only the functions/variables that are visible at the point of its instantiation? – Ignat Loskutov Mar 03 '16 at 13:56
  • Ignat, I've updated my answer with more details on how to solve the problem. – CodeFuller Mar 03 '16 at 14:11
  • But if I had to "have all instantiations that will probably be used, within this module", it wouldn't work after replacing `id(0)` with `0` as well, but it does! – Ignat Loskutov Mar 03 '16 at 14:37