4

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?

sj755
  • 3,944
  • 14
  • 59
  • 79
  • 3
    The `` type is part of the upcoming revised C++ standard and might be supported in g++ if you try changing the language to C++0x. I'm not sure if this will work, but this is probably the cause of the problem. – templatetypedef Jun 02 '11 at 00:02
  • 3
    In other words, try `g++ -std=c++0x` – Nemo Jun 02 '11 at 00:13
  • @Nemo I'll try that out tomorrow, but for now, I'm happy using pairs (as recommended in the answer). Thanks. – sj755 Jun 02 '11 at 00:24

2 Answers2

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.

PeterT
  • 7,981
  • 1
  • 26
  • 34
  • Of course, I've been using tuples so much I forgot that two elements can be contained in a pair. – sj755 Jun 02 '11 at 00:23
1

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

sehe
  • 374,641
  • 47
  • 450
  • 633