Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error while using std::basic_stringstream<char16_t> in VC++14

I am trying to do some baic char16_t string (u16string) processing, and have ran into some troubles. This short program:

#include <string>
#include <sstream>

int main()
{
    int foo = 65;

    std::basic_stringstream<char16_t> ss;
    ss << foo;

    std::u16string s = ss.str();
}

Creates the error:

Error   C2491   'std::numpunct<_Elem>::id': definition of dllimport static data member not allowed. xlocnum 259

I have tried this on some online compilers, but there is no error there.

Thanks for any help I can get!

like image 314
Russell Greene Avatar asked Oct 19 '22 04:10

Russell Greene


1 Answers

OK, it looks like a bug in the VC++ standard libraries or in the VC++ compiler, or perhaps even both.

<xlocnum>, line 85, declares within class numpunct:

__PURE_APPDOMAIN_GLOBAL _CRTIMP2_PURE static locale::id id; // unique facet id

<xlocnum>, line 258/259 defines:

template<class _Elem>
    __PURE_APPDOMAIN_GLOBAL locale::id numpunct<_Elem>::id;

_CRTIMP2_PURE is defined as _CRTIMP2, which in turn is defined as __declspec(dllimport).

Now, according to my reading of the VC++ documents, that should be OK. __declspec(dllimport) is permitted on static declarations. It isn't, however, permitted on static definitions. But the definition doesn't have the __declspec(dllimport), only the declaration does.

Nonetheless, an error is being produced: the compiler is seeing the definition, treating it as if it were __declspec(dllimport), and producing an error.

The reason I'm not sure if it's a compiler error or a library error is that the compiler also emits a warning to complain that the declaration and definition don't match--that one is __declspec(dllimport) and the other is not. Since the definition cannot, per the documentation, be __declspec(dllimport), that suggests to me that neither the declaration nor the definition should be __declspec(dllimport).

If we look at other similar members, this suspicion is confirmed. For example, num_get::id is not _CRTIMP2_PURE, nor is num_put::id.

So there are I think two possibilities. One is that the _CRTIMP2_PURE is in error, and that it should be removed. The other is that the compiler is issuing a faulty diagnostic when it claims that the definition is __declspec(dllimport), when it isn't.

Either way, I think that the code sample should compile and this is something that Microsoft will need to fix.

like image 73
DrPizza Avatar answered Nov 13 '22 19:11

DrPizza