C++ string template library

后端 未结 10 1115
清酒与你
清酒与你 2020-12-16 01:23

I want simple C++ string based template library to replace strings at runtime.

For example, I will use

string template = \"My name is {{name}}\";
         


        
10条回答
  •  佛祖请我去吃肉
    2020-12-16 01:51

    Have you considered a set of inline functions that use ostringstram instead of "string templates"?

    inline std::string name_template(const std::string& name)
    {
        std::ostringstream os;
        os << "My name is " << name;
        return os.str();
    }
    

    There are other alternate approaches if you need more generality. For example a class hierarchy where the base provides a "format" interface and child classes implement it with the appropriate varying implementation.

提交回复
热议问题