Cleaning up a switch statement [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a container class that contains a char array and an enum/integer indicating what the char array should be cast to. I use it in a callback as so:
void callback(Foo& foo){
switch(foo.type()){
case(1): do( (Bar1*) foo.stuff() );
case(2): do( (Bar2*) foo.stuff() );
case(3): do( (Bar3*) foo.stuff() );
...
}
}
Is there a way to store a mapping from the integer to the type (1, Bar1), (2, Bar2), etc. so that I can clean this switch statement up since it's getting long? Or otherwise, are there any template metaprogramming idioms that can be used in this case?

Store your objects in a std::variant instead of rolling your own using an array of char. Along with std::visit, it supports exactly the use-case you need:
void callback(std::variant<Foo1, Foo2, Foo3>& foo) {
std::visit([](auto& f) { doit(f); }, foo);
}
If you can't use std::variant then a switch is likely the simplest solution given a single known set of types.

Related

Returning pointer to an array of arrays created using new [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have created an array of arrays using char (*H)[N] = new char[M][N];
I want to return this pointer to my main function. My question is what should the function return type be in this case? Am I allowed to have a return type as a pointer to an array of arrays.
Am I allowed to have a return type as a pointer to an array of arrays.
Yes, this would be the syntax for your particular case:
char (*get_array())[N] {
return H;
}
But you really should consider using either std::unique_ptr<char[N]> or std::array<std::array<char, N>, M>.
Am I allowed to have a return type as a pointer to an array of arrays.
Yes. One way to do that would be to define a type alias and use it as the return type.
using MyArrayPointer = char (*)[N];
MyArrayPointer foo()
{
auto ptr = new char[M][N];
return ptr;
}

What's the best way to make an std::string out of one char? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
There are plenty ways to make an std::string from one char.
std::string(1, ch)
std::string() + ch
std::string(&ch, 1)
std::string {ch} \\ c++11
I wonder which one I should prefer.
Remember that source code communicates to readers, not primarily to the compiler.
Thus you should strive for clarity, and mostly leave the optimizations to the compiler.
Therefore, as the one expression that doesn't involve extraneous issues, and therefore communicates the intent most clearly, std::string{ ch } is preferable.
"A picture speaks a thousand words", as one saying goes.
The c++ equivalent might be "the interface declaration ought to be a picture".
We can create a very lightweight function, which will almost certainly be inlined wherever it's used, adding no overhead and telling the complete story:
namespace notstd {
using std::to_string;
// interface conveys all the information we need.
inline std::string to_string(char c)
{
// implementation is not actually that important
return { c };
}
}
The use case then becomes self-explanatory code:
auto s = notstd::to_string('c');
and it can be used in template-land:
template<class T>
doSomething(T const& v)
{
using notstd::to_string;
auto s = to_string(v); // will also use ADL when necessary
somethingElse(s);
}

type casting the arguments of a function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
If the code is like the below,
void func(std::string str)
{
...
}
void main()
{
std::string p1 = "abcd";
char p2[SOME_LENGTH] = "abcd";
func(p1); // (1)
func(p2); // (2)
}
which way is efficient between (1) and (2)?
They are equally efficient/inefficient. Both involves copying the string and using the copy as the value of the argument 'str'. A better way would be declaring func as
void func(const std::string &str) {
}
This can avoid copying of the string.

Use keyword as C function parameter or C++ template argument [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is it possible to use just a plain, arbitrary keyword as a C function argument or a C++ template argument?
For example:
func(Random);
or
Class<Random>();
In this example, Random is not a predefined variable name, type name or Macro value.
Essentially, these values will be converted into a string but for the my purposes, I don't want to pass it as a string.
Is this at all possible?
You can, if you want, define some macro
#define KW(x) #x
and then you are free to use
void func(const char *p);
func(KW(oi));
or
void func(std::string s);
func(KW(ciao));
... another option would be defining func as a macro, like:
#define func(a, x, b) realFunc(a, #x, b)
and then
void realFunc(int a, std::string s, double b);
func(3, arigato, 4.1);
...

Passing different inputs into overloaded functions [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I'm trying to pass user input into an overloaded function, but one functions needs a string and the other needs an int, is there a way to just pass in the user input without having to do ifs to check if its a string or int and then having to different call lines?
There is two ways to apporach your problem
1: using the template typename
template <typename T>
void print(T input){
cout<<input<<endl;
}
2: You can use the overloaded functions
void print(int input){
cout<<input<<endl;
}
void print(double input){
cout<<input<<endl;
}
Good Luck