Tuple templates in GCC

橙三吉。 提交于 2019-12-10 15:49:58

问题


I first started C++ with Microsoft VC++ in VS2010. I recently found some work, but I've been using RHEL 5 with GCC. My code is mostly native C++, but I've noticed one thing...

GCC doesn't appear to recognize the <tuple> header file, or the tuple template. At first I thought maybe it's just a typo, until I looked at cplusplus.com and found that the header is indeed not part of the standard library.

The problem is that I like to write my code in Visual Studio because the environment is way superior and aesthetically pleasing than eclipse or netbeans, and debugging is a breeze. The thing is, I've already written a good chunk of code to use tuples and I really like my code. How am I supposed to deal with this issue?

Here's my code:

using std::cout;
using std::make_tuple;
using std::remove;
using std::string;
using std::stringstream;
using std::tolower;
using std::tuple;
using std::vector;

// Define three conditions to code
enum {DONE, OK, EMPTY_LINE};
// Tuple containing a condition and a string vector
typedef tuple<int,vector<string>> Code;


// Passed an alias to a string
// Parses the line passed to it
Code ReadAndParse(string& line)
{

    /***********************************************/
    /****************REMOVE COMMENTS****************/
    /***********************************************/
    // Sentinel to flag down position of first
    // semicolon and the index position itself
    bool found = false;
    size_t semicolonIndex = -1;

    // Convert the line to lowercase
    for(int i = 0; i < line.length(); i++)
    {
        line[i] = tolower(line[i]);

        // Find first semicolon
        if(line[i] == ';' && !found)
        {
            semicolonIndex = i;
            // Throw the flag
            found = true;
        }
    }

    // Erase anything to and from semicolon to ignore comments
    if(found != false)
        line.erase(semicolonIndex);


    /***********************************************/
    /*****TEST AND SEE IF THERE'S ANYTHING LEFT*****/
    /***********************************************/

    // To snatch and store words
    Code code;
    string token;
    stringstream ss(line);
    vector<string> words;

    // A flag do indicate if we have anything
    bool emptyLine = true;

    // While the string stream is passing anything
    while(ss >> token)
    {
        // If we hit this point, we did find a word
        emptyLine = false;

        // Push it onto the words vector
        words.push_back(token);
    }

    // If all we got was nothing, it's an empty line
    if(emptyLine)
    {
        code = make_tuple(EMPTY_LINE, words);
        return code;
    }


    // At this point it should be fine
    code = make_tuple(OK, words);
    return code;
}

Is there anyway to save my code from compiler incompatibility?


回答1:


As long as it's just a pair you can use

typedef pair<int,vector<string>> Code;

But I don't think tuple is standard C++ (turns out it is included in TR1 and consequently also standard C++0x). As usual Boost has you covered though. So including:

#include "boost/tuple/tuple.hpp"

will solve your problem across compilers.




回答2:


Compilers that ship the TR1 library should have it here

#include <tr1/tuple.hpp>

//...

std::tr1::tuple<int, int> mytuple;

Of course for portability you can use the boost library version in the meantime



来源:https://stackoverflow.com/questions/6209067/tuple-templates-in-gcc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!