user1476999
user1476999

Reputation: 187

select an union member depending on a template parameter

I'm dealing with an union in C++, and I would like have a function template which access to the active union member depending on a template parameter.

Code is something like (doSomething is just an example):

union Union {
  int16_t i16;
  int32_t i32;
};

enum class ActiveMember {
    I16 
  , I32
}

template <ActiveMember M>
void doSomething(Union a, const Union b) {
  selectMemeber(a, M) = selectMember(b, M);
  // this would be exactly (not equivalent) the same
  // that a.X = b.X depending on T.
}

To accomplish this I only found bad hacks like specialization, or a not homogeneous way to access and assign.

I'm missing something, and such things should be do it with other approach?

Upvotes: 7

Views: 5627

Answers (3)

PaperBirdMaster
PaperBirdMaster

Reputation: 13320

The only solution that i can think about is to add operators to the union:

union Union
{
    char c;
    short s;
    int i;
    float f;
    double d;

    operator char() const { return c; }
    operator short() const { return s; }
    operator int() const { return i; }
    operator float() const { return f; }
    operator double() const { return d; }
    template <typename T> operator T() const { /* invalid conversion */ T t; return t; }

    Union &operator =(char ac) { c = ac; return *this; }
    Union &operator =(short as) { s = as; return *this; }
    Union &operator =(int ai) { i = ai; return *this; }
    Union &operator =(float af) { f = af; return *this; }
    Union &operator =(double ad) { d = ad; return *this; }
    template <typename T> Union &operator =(T at) { /* invalid asignement */ return *this; }
};

It allows you to control the behaviour of the union when it works as a some type:

template <typename T>
void doSomething(Union a, const Union b)
{
    // call the 'b' conversion operator and the 'a' asignment operator.
    a = static_cast<T>(b);
}

int main(int argc, char **argv)
{
    Union a, b;

    doSomething<int>(a, b);  // calls a.i = b.i
    doSomething<char>(a, b); // calls a.c = b.c

    return 0;
}

The template version of the operators matches the invalid conversions.

Upvotes: 1

Philipp
Philipp

Reputation: 11814

Possibility 1

instead of using an enum, you can use simple structs to pick the member:

typedef short int16_t;
typedef long int32_t;

union Union {
    int16_t i16;
    int32_t i32;
};

struct ActiveMemberI16 {};
struct ActiveMemberI32 {};

template <typename M>
void doSomething(Union& a, Union b) {
    selectMember(a, M()) = selectMember(b, M());

    // this would be exactly (not equivalent) the same
    // that a.X = b.X depending on T.
}

int16_t& selectMember(Union& u, ActiveMemberI16)
{
    return u.i16;
}

int32_t& selectMember(Union& u, ActiveMemberI32)
{
    return u.i32;
}

int main(int argc, char* argv[])
{
    Union a,b;
    a.i16 = 0;
    b.i16 = 1;
    doSomething<ActiveMemberI16>(a,b);
    std::cout << a.i16 << std::endl;

    b.i32 = 3;
    doSomething<ActiveMemberI32>(a,b);
    std::cout << a.i32 << std::endl;
    return 0;
}

This requires to define a struct and a selectMember method for every member in the union, but at least you can use selectMember across many other functions.

Note that I turned the arguments to references, you might adjust this if not appropriate.

Possibility 2

By casting the union pointer to the desired type pointer, you can go with a single selectMember function.

typedef short int16_t;
typedef long int32_t;

union Union {
    int16_t i16;
    int32_t i32;
};
template <typename T>
T& selectMember(Union& u)
{
    return *((T*)&u);
}

template <typename M>
void doSomething(Union& a, Union b) {
    selectMember<M>(a) = selectMember<M>(b);

    // this would be exactly (not equivalent) the same
    // that a.X = b.X depending on T.
}



int _tmain(int argc, _TCHAR* argv[])
{
    Union a,b;
    a.i16 = 0;
    b.i16 = 1;

    doSomething<int16_t>(a,b);
    std::cout << a.i16 << std::endl;

    b.i32 = 100000;
    doSomething<int32_t>(a,b);
    std::cout << a.i32 << std::endl;
    return 0;
}

Upvotes: 7

Andy Prowl
Andy Prowl

Reputation: 126432

I'm not sure why you consider template specialization a "bad hack", but there is no such a thing as "static if" in C++, so if you want your compiler to differentiate the code produced based on the result of an expression evaluated at compile-time, you need to define different, specialized versions of the template.

Here is how you would define it:

#include <iostream>

using namespace std;

union Union {
  int16_t int16;
  int32_t int32;
};

enum class ActiveMember {
    INT16
  , INT32
};

// Declare primary template
template <ActiveMember M>
void doSomething(Union a, const Union b);

// First specialization
template <>
void doSomething<ActiveMember::INT16>(Union a, const Union b)
{
    a.int16 = b.int16;

    // Do what you want here...
    cout << "int16" << endl;
}

// Second specialization
template <>
void doSomething<ActiveMember::INT32>(Union a, const Union b)
{
    a.int32 = b.int32;

    // Do what you want here...
    cout << "int32" << endl;
}

And this is how you would use it.

int main()
{
    Union u1, u2;
    u1.int32 = 0;
    u2.int32 = 0;

    doSomething<ActiveMember::INT16>(u1, u2);
    doSomething<ActiveMember::INT32>(u1, u2);

    return 0;
}

Upvotes: 3

Related Questions