4

Given following code,

#include <sstream>
#include <stdint.h>

template <typename D> void func() {
    std::basic_stringstream<D> outStream;
    D suffix = 0;
    outStream << suffix;
}

void main() {
    func<char>();     // OK
    func<wchar_t>();  // OK
    func<uint16_t>(); // generates C2491
}

what does following compile error mean?

error C2491: 'std::numpunct<_Elem>::id' : definition of dllimport static data member not allowed

Ben Kircher
  • 152
  • 1
  • 7
Pinky
  • 1,217
  • 4
  • 17
  • 27
  • 4
    Sounds like a perfectly reasonable question to me (presenting a piece of short code and a compile error). I really don't know why some people rushed into closing it :( – CristiFati Feb 05 '18 at 23:35
  • Same problem here, this appears to be either a bug in the MSVC std header xlocnum or the surrounding code. – rioki Aug 23 '20 at 11:28

1 Answers1

4

You can't declare methods with

_declspec(dllimport)

and provide a definition for them.

The qualifier tells the compiler that the function is imported from a different library than the one you are compiling now, so it wouldn't make sense to provide a definition for it.

When including the header, the qualifier should be

_declspec(dllimport)

and when you are compiling the module that provides a definition for the method it should be:

_declspec(dllexport)

The usual way of doing this is:

#ifdef CURRENT_MODULE
#define DLLIMPORTEXPORT _declspec(dllexport)
#else
#define DLLIMPORTEXPORT _declspec(dllimport)
#endif

The define CURRENT_MODULE is only defined in the module that contains the definitions, so when compiling that module the method is exported. All other modules that include the header don't have CURRENT_MODULE defined and the function will be imported.

I'm guessing your directive - _declspecimport - is similar to this.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 1
    Although this answer is a guideline to fix *C2491*, it doesn't apply to the current situation (because the error message is misleading?), and doesn't fix it. I created [\[SO\]: Compile error for (char based) STL (stream) containers in Visual Studio](https://stackoverflow.com/questions/48716223/compile-error-for-char-based-stl-stream-containers-in-visual-studio) and added more details. – CristiFati Feb 14 '18 at 10:42