Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Figure out array length statically using templates in C++

Tags:

c++

templates

I'm looking to write a char buf wrapper that guarantees null-termination like the following:

template<size_t N> class BufStr
{
public:
    explicit BufStr(const char(&buf)[N])
    {
        memcpy(m_buf, buf, N);
        m_buf[N-1] = '\0';
    }

    inline const char* c_str() const { return m_buf; }

protected:
    char m_buf[N];
};

But I'd like to use the signature template and be able to pass in a char buf directly to the constructor which would be able to determine the size of array using sizeof therefore computing N at compile time.

like image 933
stgtscc Avatar asked Apr 18 '26 01:04

stgtscc


1 Answers

Edited To account for the fact that you want to 'wrap' non-zero-terminated char arrays:

You could have a 'factory' function:

template <size_t N> 
   BufStr<N+1> make_bufstr(const char(&buf)[N])
{
    return BufStr<N+1>(buf);
}

Demo (note the use of std::copy instead of memcpy):

#include <cstdint>
#include <algorithm>

template<std::size_t N> class BufStr
{
public:
    explicit BufStr(const char(&buf)[N-1])
    {
        static_assert(N>0, "illegal size");
        std::copy(buf, buf+N-1, m_buf);
        m_buf[N-1] = '\0';
    }

    inline const char* c_str() const { return m_buf; }

protected:
    char m_buf[N];
};

template <size_t N> 
   BufStr<N+1> make_bufstr(const char(&buf)[N])
{
    return BufStr<N+1>(buf);
}

int main()
{
    const char raw[] = { 'H', 'e', 'l', 'l', 'o' }; // no NUL termination!
    auto s = make_bufstr(raw); // adds terminating NUL
}
like image 126
sehe Avatar answered Apr 19 '26 14:04

sehe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!