Timothy Madden <
termin...@gmail.com> wrote:
> Hello
>
> I am writing a class template for fixed capacity strings:
> basic_static_string<SIZE, char, char_traits<char>>
> (similar to the one in boost, but my company doesn't use newest version of
> boost yet). I use C++14 at work, and the only data members of my class
> template are:
> std::size_t len;
> char buffer[SIZE];
>
> When I instantiate and construct a static string, like:
> static_string<256u>("Delta")
> I would like to only initialize the needed characters (characters 0 to 5)
> and leave the other 251 characters uninitialized.
>
> I would like my class to be constexpr, since there is no memory
> allocation. However, somehow a constexpr constructor must use the member-
> initializers to initialize the entire object.
You mean that you wouldn't want to have the initializer that I have marked
below with "// THIS"?
//---------------------------------------------------------------------
#include <cstddef>
template<std::size_t kCapacity>
class StaticString
{
char mData[kCapacity];
std::size_t mLength;
public:
template<std::size_t kStrLength>
constexpr StaticString(const char(&initStr)[kStrLength]):
mData{}, // THIS
mLength(kStrLength - 1)
{
for(std::size_t i = 0; i < kStrLength; ++i)
mData[i] = initStr[i];
}
constexpr const char* c_str() const { return mData; }
};
#include <iostream>
int main()
{
constexpr StaticString<256> str("hello world");
std::cout << "\"" << str.c_str() << "\"\n";
}
//---------------------------------------------------------------------