tags:

views:

252

answers:

3

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

+4  A: 

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
OP specifically asked how to use the VS2010 implementation, yes?
Steve Townsend
@Steve Townsend yes, but i personally like SO that not all answers give direct answer but somehow alternative
Andrey
@Steve: No, he said "how use [TR1] in visual c++ 2010" which I think most people would interpret as "how to use *an* implementation of TR1 *with* vs2010" as opposed to "how to use *the* implementaion of TR1 *that comes with* vs2010".
James Curran
Fair enough, thanks James/Andrey
Steve Townsend
+2  A: 

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
"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
@Mike: I don't know anything about VS. I only answered because the question was about to be closed.
Potatoswatter
+4  A: 

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