How do I use a C++ TR1 library in visual c++ 2010?
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.
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!
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);
}