4

How do I use a C++ TR1 library in visual c++ 2010?

Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
user439547
  • 189
  • 1
  • 6
  • 12
  • Please read my answer and TR1 citation before voting to close. – Potatoswatter Sep 09 '10 at 19:28
  • No, this is StackOverflow. Questions should be answerable questions, with notable exceptions (e.g. "subjective"). It would *really* help if the questioner referred to a *specific part* of TR1 that he/she/it is trying to use, so that a specific answer (preferably with an example) can be given. – Mike DeSimone Sep 09 '10 at 20:01
  • @Mike: You read my answer… what don't you understand? TR1 provides for hiding itself behind a directory or a `#define`. If neither of those applies, that is the answer. – Potatoswatter Sep 09 '10 at 20:43
  • There we go. It is now in the form of a question. – jalf Sep 09 '10 at 22:49

3 Answers3

5

VS2010 comes with a few C++0x features built-in. Some features of TR1, such as the mathematical functions, are not included in the Visual C++ implementation of TR1.

boost has an implementation of TR1, you can get it by downloading boost.

To disable the C++0x/TR1 headers from VS2010 and use the boost implementation, define _HAS_CPP0X=0 in the project settings for your VS2010 project.

Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
4

If you want to use the implementation of TR1 that is packaged with VS10, it is a simple matter of simply #including the headers you need and hit the ground running. Not all of TR1 is included in the VS10 implementation of TR1. You can find a list of which parts of TR1 (and C++0x as a whole) are included in the factory-supplied implementation here, and here is a simplistic example of how to use regexes in VS10 as taken from an MSDN sample page:

// std_tr1__regex__regex_search.cpp 
// compile with: /EHsc 
#include <regex> 
#include <iostream> 

int main() 
    { 
    const char *first = "abcd"; 
    const char *last = first + strlen(first); 
    std::cmatch mr; 
    std::regex rx("abc"); 
    std::regex_constants::match_flag_type fl = 
        std::regex_constants::match_default; 

    std::cout << "search(f, f+1, \"abc\") == " << std::boolalpha 
        << regex_search(first, first + 1, rx, fl) << std::endl; 

    std::cout << "search(f, l, \"abc\") == " << std::boolalpha 
        << regex_search(first, last, mr, rx) << std::endl; 
    std::cout << "  matched: \"" << mr.str() << "\"" << std::endl; 

    std::cout << "search(\"a\", \"abc\") == " << std::boolalpha 
        << regex_search("a", rx) << std::endl; 

    std::cout << "search(\"xabcd\", \"abc\") == " << std::boolalpha 
        << regex_search("xabcd", mr, rx) << std::endl; 
    std::cout << "  matched: \"" << mr.str() << "\"" << std::endl; 

    std::cout << "search(string, \"abc\") == " << std::boolalpha 
        << regex_search(std::string("a"), rx) << std::endl; 

    std::string str("abcabc"); 
    std::match_results<std::string::const_iterator> mr2; 
    std::cout << "search(string, \"abc\") == " << std::boolalpha 
        << regex_search(str, mr2, rx) << std::endl; 
    std::cout << "  matched: \"" << mr2.str() << "\"" << std::endl; 

    return (0); 
    } 
John Dibling
  • 99,718
  • 31
  • 186
  • 324
2

Unlike GCC, the TR1 headers in VC2010 are not sequestered in a TR1/ directory. I know this not from using VC but because someone told me that GCC's implementation is unusual in this fashion.

N1836 1.3/4:

It is recommended either that additional declarations in standard headers be protected with a macro that is not defined by default, or else that all extended headers, including both new headers and parallel versions of standard headers with nonstandard declarations, be placed in a separate directory that is not part of the default search path.

So, you might also need add a #define. It is unfortunate that they didn't standardize this!

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • 1
    "Might"? Given that we're only talking about VS2010, are there other things that need to be known to determine if you need said `#define`? – Mike DeSimone Sep 09 '10 at 20:02
  • @Mike: I don't know anything about VS. I only answered because the question was about to be closed. – Potatoswatter Sep 09 '10 at 20:30