# HDF5 Union/Variant Support

**URL:** <https://forum.hdfgroup.org/t/hdf5-union-variant-support/12976>\
**Category:** C++ Users' Group\
**Created:** [January 15, 2025, 6:11pm UTC](https://forum.hdfgroup.org/t/hdf5-union-variant-support/12976 "2025-01-15T18:11:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zarahtaufique](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/z/e47c2d/32.png) [@zarahtaufique](https://forum.hdfgroup.org/u/zarahtaufique)\
**Post date:** [January 15, 2025, 6:11pm UTC](https://forum.hdfgroup.org/t/hdf5-union-variant-support/12976/1 "2025-01-15T18:11:54Z")

</div>

Hello! I have defined a compound datatype in HDF5 that is closely tied to a struct. To save memory, I was considering adding a union or variant to the struct. However, I anticipate that this might cause issues with the compound datatype.

Does HDF5 provide support for unions or variants, or has anyone encountered a similar situation in the past?

---

<div class="post-metadata">

**Author:** ![contact](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/contact/32/1067_2.png) [@contact](https://forum.hdfgroup.org/u/contact)\
**Post date:** [January 16, 2025, 1:19pm UTC](https://forum.hdfgroup.org/t/hdf5-union-variant-support/12976/3 "2025-01-16T13:19:14Z")

</div>

Hi @zarahtaufique,

The use-case you described could be solved through an `opaque` data type (as HDF5 does not support C/C++ `union` data type natively).

Using [HDFql](https://www.hdfql.com), a high-level (declarative) programming language to manage HDF5 data, your use-case could be solved as follows in C++:

```auto
// include necessary C++ header files
#include <cstdlib>
#include <iostream>
#include "HDFql.hpp"

// define an union named "Data"
union Data
{
	int i;
	double d;
	char c[12];
};

// implement program
int main(int argc, char *argv[])
{

	// declare variables used in the program
	union Data data;
	std::ostringstream script;

	// create an HDF5 file named 'test.h5' and use (i.e. open) it
	HDFql::execute("CREATE AND USE FILE test.h5");

	// create a dataset named 'dset' of type opaque with a size equal to the size of variable 'data'
	script << "CREATE DATASET dset AS OPAQUE(" << sizeof(data) << ")";
	HDFql::execute(script);

	// register variable 'data' for subsequent usage (by HDFql)
	HDFql::variableRegister(&data);

	// write 'data' as an integer into dataset 'dset'
	data.i = 100;
	HDFql::execute("INSERT INTO dset VALUES FROM MEMORY 0");

	// populate 'data' (from dataset 'dset') and print it as an integer
	memset((void *) &data, 0, sizeof(data));
	HDFql::execute("SELECT FROM dset INTO MEMORY 0");
	std::cout << data.i << std::endl;

	// write 'data' as a double into dataset 'dset'
	data.d = 250.7;
	HDFql::execute("INSERT INTO dset VALUES FROM MEMORY 0");

	// populate 'data' (from dataset 'dset') and print it as a double
	memset((void *) &data, 0, sizeof(data));
	HDFql::execute("SELECT FROM dset INTO MEMORY 0");
	std::cout << data.d << std::endl;

	// write 'data' as a char into dataset 'dset'
	strcpy(data.c, "Hello world!");
	HDFql::execute("INSERT INTO dset VALUES FROM MEMORY 0");

	// populate 'data' (from dataset 'dset') and print it as a char
	memset((void *) &data, 0, sizeof(data));
	HDFql::execute("SELECT FROM dset INTO MEMORY 0");
	std::cout << data.c << std::endl;

	return EXIT_SUCCESS;

}

```

Hope it helps!
