Satisfying a template variable call

I have the following piece of C ++ code:

template_par<std::string> a("name", "Joh Node");
template_par<int> b("age", 23);
std::string result = templater<SomeTemplateClass>().templatize(a, b).get();

      

Which is trying to implement the templating engine for various purposes. The important parts of the class templater

are:

template<class T>
class templater
{
    std::map<std::string, std::string> kps;

public:

    template <typename T1>
    templater& templatize(template_par<T1> b)
    {
        kps.insert(make_pair(b.getKey(), to_string(b.getValue())));
        return *this;
    }


    template<typename T1, typename... Args1>
    templater& templatize(T1 first, Args1... args)
    {
        kps.insert(make_pair(first.getKey(), to_string(first.getValue())));
        return templatize(args...);
    }
}

      

i.e. the template function with variable arguments .... template_par<T>

is just the template parameter classes for the base stuff. Anyway, it works, it does the job beautifully.

However, I would like to somehow shorten the way I call the method, templatize

not only for aesthetics, but also for calling ... I think it would look much nicer:

  std::string result = templater<SomeTemplateClass>().templatize(
            "name" -> "Joh Node",
            "age"  -> 42
  );

      

However, this approach is not possible due to being operator ->

a somewhat harsh part of C ++ ... ( std::string result = templater<SomeTemplateClass>().templatize

here is not an important role to hide in a friendly construct, I'm more worried about the variable number of parameters)

Any good landscaping ideas for the call above?

+3


source to share


1 answer


Take a look at Boost.Assign , specifically part of its purpose that you could co-opt here:

std::string result = templater<SomeTemplateClass>()
    ("Name", "Joh Node")
    ("Age", 42).templatize();

      

I wouldn't get much more creative than this, which makes the code cryptic. That said, if you want to experiment wildly, you might like my operator names which allow for syntax like:



std::string result = templater<SomeTemplateClass>().templatize(
    "name" <is> "Joh Node",
    "age"  <is> 42
);

      

There is

can be any valid C ++ identifier. Thus, the usual operators are unfortunately absent, but almost everything flies. Even if you really want to click <_>

.

+2


source







All Articles