#include <cassert>
#ifdef WOULD_TR1_REGEX_BE_COMPLETE
#include <tr1/regex> //std::tr1::regex
#endif
#include <tr1/array> //std::tr1::array
#include <tr1/memory> //std::tr1::shared_ptr
int main()
{
//std::tr1::array
{
//const std::tr1::array<int,3> w = {{ 0,1 }}; //Does compile: too few elements is not checked in assignment
//const std::tr1::array<int,3> x = {{ 0,1,2,3 }}; //Does not compile: too many elements is checked in assignment
const std::tr1::array<int,3> v = {{ 0,1,2 }}; //Note the double braces
//static_assert(v.size() == 3,""); //Not allowed
assert(v[1] == 1);
}
//std::tr1::regex
{
#ifdef WOULD_TR1_REGEX_BE_COMPLETE
//Define how a dutch zip code is formatted
const std::tr1::regex dutch_zip_code("\\d{4}\\s[A-Z]{2}");
//Check if the regex works properly
assert(std::tr1::regex_match("1234 AB",dutch_zip_code)==true);
assert(std::tr1::regex_match("1234 ab",dutch_zip_code)==false);
assert(std::tr1::regex_match("1234ab",dutch_zip_code)==false);
//Define a sentence with a Dutch zip code in it
const std::string s = "My Dutch zip code is 1234 AB.";
//Show how std::tr1::regex_match and std::tr1::regex_search work
assert(std::tr1::regex_match(s,dutch_zip_code)==false
&& "the std::string does not match a dutch zip code");
assert(std::tr1::regex_search(s,dutch_zip_code)==true
&& "but the std::string does contain a dutch zip code");
//Show how to obtain a Dutch zip code from a std::string
/* const */ std::tr1::sregex_iterator i(s.begin(),s.end(),dutch_zip_code);
const std::string t = (*i).str();
assert(t=="1234 AB");
#endif
}
//std::tr1::shared_ptr
{
std::tr1::shared_ptr<int> p(new int(123));
assert(p);
assert(*p == 123);
std::tr1::shared_ptr<int> q = p;
assert(q);
assert(*p == *q);
*p = 234;
assert(*q == 234);
}
}
|