Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional variable declaration

I'm coming from Python and I have some problem with managing types in c++. In Python I can do something like this:

if condition_is_true:
    x=A()
else:
    x=B()

and in the rest of the program I can use x without caring about the type of x, given that I use methods and member variables with the same name and arguments (not necessary that A and B have the same base classes). Now in my C++ code type A corresponds to

typedef map<long, C1> TP1;

and B to:

typedef map<long, C2> TP2;

where:

typedef struct C1
{
    char* code;
    char* descr;
    int x;
...
}

and

typedef struct C2
{
    char* code;
    char* other;
    int x;
...
}

C1 and C2 have similar members and in the part of code I'm talkin of I only have to use the ones with the same name/type

I would like to do something like:

if (condition==true)
{
    TP1 x;
}
else
{
    TP2 x;
}

what is the correct approach in c++?

thanks in advance

like image 259
user862989 Avatar asked Jul 26 '11 08:07

user862989


People also ask

Can you declare a variable in an if statement C++?

Starting in C++17, an if statement may also contain an init-statement expression that declares and initializes a named variable. Use this form of the if-statement when the variable is only needed within the scope of the if-statement.

Can you declare variables in an if statement?

Variable scope. Java allows you to declare variables within the body of a while or if statement, but it's important to remember the following: A variable is available only from its declaration down to the end of the braces in which it is declared.

Can we assign value in if condition?

Yes, you can assign the value of variable inside if.

What is conditional statement in Javascript?

Conditional StatementsUse if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false.


2 Answers

If the condition is known at compile-time, you can use std::conditional. This is useful in generic code.

typedef std::conditional<
    std::is_pointer<T>::value
    , TP1
    , TP2
>::type map_type;
map_type x;

(where the test is made-up; here we're testing whether T is a pointer type or not)

If the condition cannot be known until runtime, then some form of dynamic polymorphism is needed. Typical instances of such polymorphism in C++ are subtyping, boost::variant or when push comes to shove, boost::any. Which one you should pick* and how you should apply it depends on your general design; we don't know enough.

*: very likely not to be boost::any.

like image 197
Luc Danton Avatar answered Sep 30 '22 10:09

Luc Danton


You have a couple of choices. If C1 and C2 are both POD types, you could use a union, which allows access to the common initial sequence:

struct C1 { 
    // ....
};

struct C2 { 
    // ...
};

union TP {
    C1 c1;
    C2 c2;
};

union TP x;

std::cout << x.c1.code; // doesn't matter if `code` was written via c1 or c2.

Note that to keep the initial sequence "common", you really want to change the names so the second member (descr/other) has the same name in both versions of the struct.

If they're not PODs, you can use inheritance to give you a common type.

C++, however, doesn't have a direct counterpart to Python's famous "duck typing". While templates provide type erasure (to at least some degree), you'd end up with kind of the reverse of what you're doing in Python. Instead of the variation between the two types happening where you deal with the variable, you'd allow code to deal with two different types that had common syntax. This is different, however, in that it requires that the compiler be able to resolve the actual type being used with any particular template at compile time, not just run time.

If you really need to resolve the type at run time, then templates probably won't work -- you'll probably need to use a union or base class.

like image 36
Jerry Coffin Avatar answered Sep 30 '22 08:09

Jerry Coffin