Boost logo

Boost :

Subject: Re: [boost] Heads up - string_ref landing
From: Jeffrey Yasskin (jyasskin_at_[hidden])
Date: 2012-12-10 16:16:36


On Wed, Nov 28, 2012 at 2:48 AM, Rob Stewart <robertstewart_at_[hidden]>
wrote:
> o char const (&)[N]

This constructor is dangerous in cases like

  char space[100];
  snprintf(space, 100, "format", args);
  string_ref str(space);

so I think most of the suggestions on this list have moved toward a more
explicit but very verbose string_ref::from_literal("foo\0bar").

http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2012/n3468.pdf proposed
adding various user-defined literals to the standard library. What do you
folks think of string_ref UDLs like:

namespace boost {
namespace string_literals {
#define BOOST_DEFINE_STRING_REF_LITERAL(CharT) \
  constexpr basic_string_ref<CharT> operator"" _s( \
      const CharT* str, size_t len) { \
    return basic_string_ref<CharT>(str, len); \
  }
BOOST_DEFINE_STRING_REF_LITERAL(char);
BOOST_DEFINE_STRING_REF_LITERAL(wchar_t);
BOOST_DEFINE_STRING_REF_LITERAL(char16_t);
BOOST_DEFINE_STRING_REF_LITERAL(char32_t);
#undef BOOST_DEFINE_STRING_REF_LITERAL
}
}

using namespace boost::string_literals;
constexpr boost::string_ref glbl = "Constexpr"_s;
constexpr boost::string_ref contains_null = "Const\0expr"_s;
static_assert(contains_null.at(6) == 'e',
              "Expect string to extend past null");
static_assert(glbl.size() == sizeof("Constexpr") - 1,
              "Expect string to omit trailing null");

(Tested with Clang r163674)

?

Jeffrey


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk