3

My question is about change/set dynamically a definition of a type checking a value of a variable which can change in run time like this simple example:

void changeMode(int mode)
{
    if(mode == 1)
    {
            typedef MyType float;
    }
    else if(mode == 2)
    {
            typedef MyType double;
    }
}

int main(int argc, char *argv[])
{
    MyType (2);
    MyType A = 3;
    MyType B = 2;
    MyType C = A + B;

    return 0;
}

it's possible? It's a classic case of templates? it is possible to avoid using templates? My goal is centralize the type definition in order to be possible to switch at runtime, without the need to extend it to each class or use "templates" for each class/function that will use the given type.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user2347839
  • 69
  • 1
  • 7
  • C++ doesn't have any kind of runtime type system (other than vtables). That wouldn't make any sense. – SLaks Dec 11 '13 at 17:17

4 Answers4

4

typedef is evaluated at compile time, so no.

but you can do something like this using prepocessor:

#ifdef TYPE_FLOAT
   typedef float Type;
#else
   typedef int Type;
#endif

but keep in mind it's at compile time

hl037_
  • 3,520
  • 1
  • 27
  • 58
2

You can extract your type-dependent code to function template (or class template in more complex cases):

template <typename MyType>
void ExecuteTypeDependentCode()
{
    MyType A = 3;
    MyType B = 2;
    MyType C = A + B;
}

Then you can write the wrapper, its task is to switch between modes:

enum Modes
{
    FloatMode = 1,
    DoubleMode = 2
};

void ExecuteTypeDependentCode(Modes mode)
{
    switch (mode)
    {
        case FloatMode:
            ExecuteTypeDependentCode<float>();
            break;

        case DoubleMode:
            ExecuteTypeDependentCode<double>();
            break;
    }
}

Example of using:

int main(int argc, char *argv[])
{
    ExecuteTypeDependentCode(DoubleMode);

    return 0;
}

The process of switching can be written only once (I described this method here).

Community
  • 1
  • 1
Constructor
  • 7,273
  • 2
  • 24
  • 66
1

Typedef is evaluated statically, so you need to do something like to have a "classic case of templates" (whatever that is :))

template <int Mode>
struct Dispatch { };

template <>
struct Dispatch <0> {
    using U = int; // fancy way of saying typedef
};

template <>
struct Dispatch <1> {
    using U = char; // feel free to use typedef
};

int main() {
    Dispatch<0>::U x; // compile time constant in <> (zero)
    x = 5;
    Dispatch<1>::U y; // compile time constant in <> (one)
    y = 'c';
}
ScarletAmaranth
  • 5,065
  • 2
  • 23
  • 34
1

As other answers have pointed out, this is not possible to achieve via typedef. However, you can emulate this behavior using polymorpic wrapper classes:

#include <cstdio>


class Gen
{
public:
    static int mode;

    static Gen* get()
    {
        switch(mode)
        {
        case 1:
            return new GenFloat();
        case 2:
            return new GenDouble();
        }

        return NULL;
    }
};

int Gen::mode = 0;

class DoubleGen : public Gen
{
public:
    DoubleGen() : d(0) {}
    DoubleGen(double val) : d(val) {}

    double d;
};

class FloatGen : public Gen
{
public:
    FloatGen() : f(0) {}
    FloatGen(float val) : f(val) {}

    float f;
};




int main(void)
{
    Gen::mode = 1;

    Gen* _float = gen::get();

    gen::mode = 2;

    Gen* _double = gen::get();

    delete _float;
    delete _double;

    return 0;
}
ApproachingDarknessFish
  • 14,133
  • 7
  • 40
  • 79