Icebone1000
Icebone1000

Reputation: 1321

How to initialize a char array MEMBER from a STRING LITERAL in a constexpr trough a CTOR

So this works:

        template<size_t N>
        struct LTxt
        {
            char txt[N];
        };
        void Test1()
        {
            //LTxt<10> fromliteral = "test1"; does not work, but ok
            LTxt<10> fromlitera2 = { "test2" };
            constexpr LTxt<10> fromliteral3 = { "test3" };
        }

But the moment you write a constructor for that struct, you lose that auto "privilege". Whats the constructor implementation I can write to keep that functionality. I tried a lot of things:(the commented code doesnt work)

template<size_t N>
        struct LTxt2
        {
            char txt[N];

            //LTxt2() = default; // doesnt change anything

            template<size_t N>
            constexpr LTxt2(const char(&sz)[N])
                //: txt{ sz } // Error  C2075 array initialization requires a brace - enclosed initializer list
            {
                for (int c = 0; c < N; ++c) txt[c] = sz[c];
            }
        };
        void Test2()
        {
            LTxt2<10> fromliteral = "test1";
            //constexpr LTxt2<10> fromliteral2 = "test2";
            LTxt2<10> fromliteral3 = { "test3" };
            //constexpr LTxt2<10> fromliteral4 = { "test4" };
            LTxt2<10> fromliteral5("test5");
            //constexpr LTxt2<10> fromliteral6("test6");
            LTxt2<10> fromliteral7({ "test7" });
            //constexpr LTxt2<10> fromliteral8({ "test8" });
        }

        template<size_t N>
        struct LTxt3
        {
            char txt[N];

            constexpr LTxt3(std::initializer_list<char> list)
                //:txt(list) {}
                //:txt{ list }// {}
            {
                int c = 0;
                for (auto p = list.begin(); p != list.end(); ++p, ++c)
                    txt[c] = *p;
            }
        };
        void Test3()
        {
            //LTxt3<10> fromliteral = "test1";
            //constexpr LTxt3<10> fromliteral2 = "test2";
            //LTxt3<10> fromliteral3 = { "test3" }; //why in the name of fuck that doesnt work
            //constexpr LTxt3<10> fromliteral4 = { "test4" };
            //LTxt3<10> fromliteral5("test5");
            //constexpr LTxt3<10> fromliteral6("test6");
            //LTxt3<10> fromliteral7({ "test7" });
            //constexpr LTxt3<10> fromliteral8({ "test8" });
            LTxt3<10> fromliteral9 = { 't','e','s','t','9' };
            //constexpr LTxt3<10> fromliteral10 = { 't','e','s','t','1', '0' };
        }

        template<size_t N>
        struct LTxt4
        {
            char txt[N];

            template<typename ... Params>
            constexpr LTxt4(Params ... sz)
                : txt{ sz... }
            {}
        };


        void Test4()
        {
            //LTxt4<10> fromliteral = "test1";
            //LTxt4<10> fromliteral = { "test1" };
            //LTxt4<10> fromliteral { "test1" };
            //LTxt4<10> fromliteral("test1");
            LTxt4<10> fromliteral = { 't','e','s','t','1' };
            constexpr LTxt4<10> fromliteral2 = { 't','e','s','t','2' };
        }

Upvotes: 1

Views: 217

Answers (1)

Martin Morterol
Martin Morterol

Reputation: 2900

I came up with this:

#include <iostream>
#include <stdexcept>

template<size_t N>
struct LTxt
{
    char txt[N] {};
};


template <class Char, Char... Cs>
constexpr auto operator""_txt()
{
    LTxt<sizeof...(Cs)> text;
  
    size_t index = 0;
    auto addChar = [&](char c)
    {
        text.txt[index] = c;
        index++;
    };
    
    ((addChar(Cs)), ...);

    return text;
}

int main() {
    constexpr auto txt = "test"_txt; 
    
    for (int i = 0 ; i < 4 ; i++)
    {
        std::cout << txt.txt[i] << std::endl;
    }

}

Note: The string literal operator templates taking an argument pack of characters is an GNU extension and don't work with -pedantic-errors. Both clang and gcc support it.


I you want support the syntax:

std::cout << txt.txt << std::endl;

You need to add an '\0':

template <class Char, Char... Cs>
constexpr auto operator""_txt()
{
    LTxt<sizeof...(Cs)+1> text;
    size_t index = 0;
    auto addChar = [&](char c)
    {
        text.txt[index] = c;
        index++;
    };
    ((addChar(Cs)), ...);
    text.txt[index] = '\0';
    return text;
}

Syntax:

((addChar(Cs)), ...); is an fold expression. Before c++17 when can simulate it with this trick:

auto initializerList = {
       (
           addChar(Cs)   // real code
           , 0           // the expression is evaluated at 0 each time
       )...              // expand the parameter pack, 
       , 0               // if we do: ""_txt; 
                         // we need at least one element for the auto deduction
};
(void) initializerList; // silence warning

It works because the parameter pack expansion :

expands to comma-separated list of zero or more patterns. Pattern must include at least one parameter pack.

So it will repeat the whole addChar(Cs),0 for each char in Cs.

Upvotes: 1

Related Questions