Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

STL - what is the problem of the following code?

Tags:

c++

stl

#include "stdafx.h"
#include <string>
#include <map>
using namespace std;

class NiftyEmailProgram {
private:
    typedef map<string, string> NicknameMap;
    NicknameMap nicknames;

public:
    void ShowEmailAddress(const string& nickname) const
    {
        NicknameMap::const_iterator i = nicknames.find(nickname);

        if ( i != nicknames.end() )
        {
        }
    }

};

int main(int argc, char* argv[])
{
    printf("Hello World!\n");
    return 0;
}

When I compile the above code in VC6.0, I have seen tons of warnings. If I use Warning Level 4 and treat all warnings as error, then the output error from STLFilt is as follows:

Compiling...
t3.cpp
c:\devstudio_6.0\vc98\include\xtree(118): error C2220: warning treated as error - no object file generated
    c:\devstudio_6.0\vc98\include\map(46): see reference to class template instantiation 'map<string,string>' being compiled
    C:\TEMP\t3\t3.cpp(12): see reference to class template instantiation 'map<string,string>' being compiled
Error executing cl.exe.  


t3.exe - 1 error(s), 26 warning(s)
Tool returned code: 0

Now, what is the problem of this code and how do I fix it?

Thank you

like image 257
q0987 Avatar asked Dec 17 '10 20:12

q0987


2 Answers

Try to post the non-treated warnings.

However, I too remember that I got some level 4 warning from <xtree> in <map> , that could be safely ignored (IIRC it was C4702, which is harmless).

To avoid the warning, I put around the STL #includes some appropriate #pragma warning directives (enclosed in the correct #ifdefs to have them considered only on MSVC++, thanks to @Alexandre C. for reminding me of it):

#ifdef _MSC_VER
    //Disable the C4702 warning for the following headers
    #pragma warning(push)
    #pragma warning(disable:4702)
#endif // _MSC_VER
//map STL container
#include <map>
//list STL container
#include <list>
//vector STL container
#include <vector>
#ifdef _MSC_VER
    #pragma warning(pop)
#endif

You could also simply lower the warning level to 3 (or even lower) just in that section with:

#ifdef _MSC_VER
    // Lower the warning level to 3 just for this section
    #pragma warning(push, 3)
#endif
//map STL container
#include <map>
//list STL container
#include <list>
//vector STL container
#include <vector>
#ifdef _MSC_VER
    #pragma warning(pop)
#endif // _MSC_VER

For more info, see the documentation of #pragma warning.

like image 136
Matteo Italia Avatar answered Oct 11 '22 06:10

Matteo Italia


If I remember correctly, the mangled function names created by STL easily exceed some built-in limit, and this is what triggers the warning. It's unfortunate that you used STLFilt to keep us from seeing the actual warnings produced by the compiler.

The only work-around I found was to use typedefs and/or derived classes to shorten the names used in the template.

As the others have mentioned, the best and easiest fix would be to upgrade your compiler.

Edit: I tried this on my own VC6 and the error was exactly as I remembered:

c:\program files\microsoft visual studio\vc98\include\xtree(118) : warning C4786: 'std::_Tree<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,
std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::map<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::less<std::basic_string<cha
r,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >::_Kfn,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::basi
c_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
        c:\program files\microsoft visual studio\vc98\include\map(46) : see reference to class template instantiation 'std::_Tree<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::pair<std::basic_string<char,std::char_traits<
char>,std::allocator<char> > const ,std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::map<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std::char_traits<char>,std::allocator<cha
r> >,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >::_Kfn,std::less<std::basic_string<char,std::char_traits<char>,std::allocato
r<char> > >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' being compiled

The workaround I mentioned earlier isn't enough, because with the shortest class name possible the name of an iterator still exceeds 255 characters. The solution is to put this before the #include <map>:

#pragma warning(disable:4786)
like image 2
Mark Ransom Avatar answered Oct 11 '22 07:10

Mark Ransom