Skip to content

concat with std::string or std::string_view during runtime #8

@oschonrock

Description

@oschonrock

Thanks for thinking about this problem so carefully. It seems crazy that it’s so hard to make some truly compile time constant strings without heap/malloc. I have a use case similar to person on your blog I think? Just static class convenience constants to concatenate with other std::string(_view)?

Doesn’t work, because cannot be “+”()d with std::string_view (or std::string).

I am sure you can understand my intended usage. Is there a way with your lib? Or another much simpler way?

#define AK_TOOLKIT_CONFIG_USING_STRING_VIEW 1

class A {
    static constexpr auto VT100_ESC              = sstr::literal("\x1b");
    static constexpr auto VT100_CLS              = VT100_ESC + "[2J";
    static constexpr auto VT100_BOX_DRAW_ON      = VT100_ESC + "(0";
    static constexpr auto VT100_BOX_DRAW_OFF     = VT100_ESC + "(B";
    static constexpr auto VT100_UNDERLINE_ON     = VT100_ESC + "[4m";
    static constexpr auto VT100_UNDERLINE_OFF    = VT100_ESC + "[0m";

   void method();
}

void A::method() {
    std::string_view somestr = "hello";
    std::string_view vt = VT100_UNDERLINE_ON + somestr + VT100_UNDERLINE_OFF;
    std::cout << vt;
}

compile fails with:

error: invalid operands to binary expression ('const ak_toolkit::static_str::string<4, ak_toolkit::static_str::char_array>' and 'std::string_view' (aka 'basic_string_view<char>'))
    std::string_view vt = VT100_UNDERLINE_ON + somestr + VT100_UNDERLINE_OFF;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions