New answers tagged

2 votes

std::string_view and std::string in utility library interface

std::string_view and std::string are two completely different kinds of types. std::string_view is a reference-type, or if you will a view. Like all of them, it doesn't own its data. Instead, it ...
  • 8,298
2 votes
Accepted

std::string_view and std::string in utility library interface

In C++ and its standard libs, you will find many "string like" types, like std::string, std::string_view, std::wstring, char *, and so one. This old blog post from 2008 already mentioned 30 ...
  • 188k
2 votes

Is it possible to instantiate a template class at runtime?

As the answer from Ben Cottrell explains, templates are a purely compile-time concept. They cannot be applied at runtime. If you want to provide similar functionality dynamically at runtime, you will ...
  • 126k
2 votes

Is it possible to instantiate a template class at runtime?

The only way I know to achieve what you asked for literally is to provide all 9 possible combinations of template instantiations somewhere in your code, and use this to fill a 3x3 matrix of objects ...
  • 188k
5 votes

Is it possible to instantiate a template class at runtime?

The short answer is that C++ templates do not exist anywhere in any compiled code and are not available at runtime. Template instantiation is a code generation feature of C++, and therefore strictly ...
  • 10.2k
6 votes

Optimizing a string to enum converter

First thought that comes to mind is skepticism that this is a bottleneck in your program's execution. Most arduino programs spend most of their time asleep, so you should have plenty of leftover CPU ...
  • 27.3k
6 votes
Accepted

Optimizing a string to enum converter

As already hinted at in the answer to the first question, a table might be a good idea, even if only mapping from string to enum. The code would be a bit more readable, even though not necessarily ...
3 votes

Optimizing a string to enum converter

Sounds like a good candidate for a std::map, or perhaps a std::unordered_map. If you already have a function (probably using switch) that converts the enum to string, then you could use that to ...
0 votes

Alternatives to if-else on data reading

At least to me, this looks like a fairly simple case for a simple map to handle. I'd start by defining a type to hold the command type and command param for a call to rtnProtocols: struct params { ...
2 votes

Alternatives to if-else on data reading

Command pattern The command pattern might help you. You could define one function for each cmdtype, and call the corresponding function when you receive some input via terminal. This approach was also ...
7 votes
Accepted

Alternatives to if-else on data reading

To prevent over-engineering, first boil down on what is done: if (canSend) { if (cmdtype == "VALCN") { rtnProtocols(cmdtype, "00000000"); } else if (cmdtype == &...
  • 1,891
5 votes

Alternatives to if-else on data reading

The obvious answer here is a dispatch table, i.e. something like std::map<string, function_type> (assuming you have the STL available, if you don't you can probably fake this up usefully enough ...
6 votes

Alternatives to if-else on data reading

There are some things you can do, but ultimately you may need to live with some compromise due to Arduino resource limitations. Cleanup approaches One thing is to separate the input parsing from ...

Top 50 recent answers are included