简体   繁体   中英

Error while init of C++ std map with C++ Pair ON SOLARIS

The below code runs OK in Linux but gives compiler error in Solaris. I am trying to initialize an std pair and then using it to init a C++ Map. It runs perfectly in linux , however Solaris has got an issue with this. Any one has idea what can be done to make it OK in all UNIX variations?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <iostream>
#include <map>
#define TRUE 1
#define FALSE 0

using namespace std;


std::pair<std::string, std::string> envVarsData[] =
{
        std::make_pair(std::string("HOME"), std::string("home")),
        std::make_pair(std::string("RETURNED"), std::string("Returned"))
};

size_t iSize = sizeof(envVarsData) / sizeof(envVarsData[0]);
std::map<std::string, std::string> envVarsMap(envVarsData, iSize);

int main()
{

 return 0;
}

The error thrown is as below

# CC t1.cpp
"t1.cpp", line 21: Error: Could not find a match for std::map<std::string,std::string>::map(std::pair<std::string, std::string>[2], unsigned) needed in<no tag>.
1 Error(s) detected.
#

To use C++11 features with the Solaris Studio C++ compiler, you must use the -std=c++11 option. Per the What's New in Oracle® Solaris Studio 12.4 guide :

Using C++11 Features

In Oracle Solaris Studio 12.4, the C++ compiler supports C++11, a new language and ABI (Application Binary Interface).

In C++ 11 mode, the CC compiler uses the g++ ABI and a version of the g++ runtime library that is supplied with Oracle Solaris Studio. For this release, version 4.8.2 of the g++ runtime library is used.

An ABI describes the low-level details in the generated object code. Modules that use different ABIs cannot successfully be linked together into a program. This means that you must use C++11 mode on all modules in your program, or none of them.

If you use Oracle Solaris Studio 12.4 C++ as an upgrade to Oracle Solaris Studio 12.3 (C++ 5.12), no changes are needed in scripts or makefiles if you are not using C++11 features. An exception is Rogue Wave Tools.h++ is not available. For more information about features no longer supported, see Features That Have Been Removed in This Release in Oracle Solaris Studio 12.4: Release Notes

To compile in C++11 mode, add the option –std=c++11 to the CC command line. The location on the command line is not important. The option causes the compiler to recognize language features new in C++11, and to use the C++11 version of the standard library (g++ runtime library that is supplied). Except for the options marked as incompatible with –std=c++11, all other command-line options can be used along with C++11, and have their usual effects. The –std=c++11 option must be used consistently on every CC command used in building a library or executable program.

Imagine having a huge installed base of C++ code, upgrading to a new compiler, and having the default language option change from, say, C++03 to C++11 and breaking a lot of already-working code .

It runs perfectly in linux

That's likely because the default language option for g++ is GNU-extended, non-standard C++14 :

The default, if no C++ language dialect options are given, is -std=gnu++14 .

If you want portable code, you can't use non-standard extensions on any platform.

Any one has idea what can be done to make it OK in all UNIX variations?

Don't use non-standard compiler extensions on any platform.

Yes, that means you likely have to go back and change the options on Linux, where if you are using the default g++ options you are compiling with non-standard GNU extensions to C++.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM