Is it reasonable to use std::basic_string as a contiguous buffer when targeting C++03?

I’d consider it quite safe to assume that std::string allocates its storage contiguously. At the present time, all known implementations of std::string allocate space contiguously. Moreover, the current draft of C++ 0x (N3000) [Edit: Warning, direct link to large PDF] requires that the space be allocated contiguously (§21.4.1/5): The char-like objects in a basic_string object … Read more

How to write `is_complete` template?

The answer given by Alexey Malistov can be used on MSVC with a minor modification: namespace { template<class T, int discriminator> struct is_complete { static T & getT(); static char (& pass(T))[2]; static char pass(…); static const bool value = sizeof(pass(getT()))==2; }; } #define IS_COMPLETE(X) is_complete<X,__COUNTER__>::value Unfortunately, the __COUNTER__ predefined macro is not part of … Read more

In which versions of the C++ standard does “(i+=10)+=10” have undefined behaviour?

tl;dr: The sequence of the modifications and reads performed in (i+=10)+=10 is well defined in both C++98 and C++11, however in C++98 this is not sufficient to make the behavior defined. In C++98 multiple modifications to the same object without an intervening sequence-point results in undefined behavior, even when the order of those modifications is … Read more

Is value of x*f(x) unspecified if f modifies x?

In terms of evaluation sequence, it is easier to think of x*f(x) as if it was: operator*(x, f(x)); so that there are no mathematical preconceptions on how multiplication is supposed to work. As @dan04 helpfully pointed out, the standard says: Section 1.9.15: “Except where noted, evaluations of operands of individual operators and of subexpressions of … Read more

Does “&s[0]” point to contiguous characters in a std::string?

A std::string’s allocation is not guaranteed to be contiguous under the C++98/03 standard, but C++11 forces it to be. In practice, neither I nor Herb Sutter know of an implementation that does not use contiguous storage. Notice that the &s[0] thing is always guaranteed to work by the C++11 standard, even in the 0-length string … Read more

Which Typesafe Enum in C++ Are You Using?

I’m currently playing around with the Boost.Enum proposal from the Boost Vault (filename enum_rev4.6.zip). Although it was never officially submitted for inclusion into Boost, it’s useable as-is. (Documentation is lacking but is made up for by clear source code and good tests.) Boost.Enum lets you declare an enum like this: BOOST_ENUM_VALUES(Level, const char*, (Abort)(“unrecoverable problem”) … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)