Eric Niebler wrote:
Andy Stevenson wrote:
I recall some discussion of there being an mpl::string
template..... Can't see it in the mpl library. Is it elsewhere?
It has been added to trunk as of revision 52208:
https://svn.boost.org/trac/boost/changeset/52208
No doubt the regression tests will reveal portability problems. Once
they have been worked out, we can move this to release.
Visual Studio 2005 hates it. Perhaps I'm not going about it the right
way? I simply downloaded string.hpp, char.hpp, and char_fwd.hpp and
altered the include paths so that they'd work in an individual project.
Then I wrote the following test code:
#include <iostream>
#include "string.hpp"
template < char const* str >
struct test
{
static void print() { std::cout << str << std::endl; }
};
int main()
{
typedef boost::mpl::string<'hell', 'o wo', 'rld!'> str;
std::cout << str::template at<0>::value << std::endl;
std::cin.get();
}
1>e:\dev_workspace\experimental\scratch\scratch\main.cpp(15) : error
C2976: 'boost::mpl::string::at' : too few template arguments
1> with
1> [
1> C0=1751477356,
1> C1=1864398703,
1> C2=1919706145
1> ]
This of course causes problems in c_str.
This code fails for the same reason:
#include <iostream>
#include
template < typename T >
struct test_inner
{
template < typename X, bool B = boost::is_same::value >
struct inner
{
static bool const value = B;
};
};
int main()
{
std::cout << test_inner<int>::template inner<int>::value << std::endl;
std::cin.get();
}
So, looks like VS can't handle default template arguments in these inner
templates. I changed the implementation of at<>:
template < typename String, long Pos, bool B = (Pos <
BOOST_MPL_MULTICHAR_LENGTH(String::front_)) >
struct string_at :
boost::mpl::char_
{
};
template < typename String, long Pos >
struct string_at
: string_at< typename String::rest_, Pos -
BOOST_MPL_MULTICHAR_LENGTH(String::front_)>
{
};
template<>
struct at_impl
{
template
struct apply
: string_at
{};
};
And implemented c_str[] in terms of string_at<>:
template
char const string::c_str[] =
{
#define M0(z, n, data)
string_at,
n>::value
BOOST_PP_ENUM(BOOST_MPL_STRING_MAX_LENGTH, M0, ~)
#undef M0
, '\0' // to ensure the string is null-terminated
};
This code compiles with changes in VS 2005:
#include <iostream>
#include
#include
#include "string.hpp"
int main()
{
typedef boost::mpl::string<'hell', 'o wo', 'rld!'> str;
std::cout << boost::mpl::at >::value <<
std::endl;
std::cout << str::c_str << std::endl;
std::cin.get();
}