---
title: "Structures"
url: https://docs.zeroc.com/ice/3.8/slice/user-defined-types/structures
language: cpp
docs_index: https://docs.zeroc.com/llms.txt
---

> For an index of every page, see [llms.txt](https://docs.zeroc.com/llms.txt).

# Structures

Programming language: C++. Other languages: [C#](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/structures/csharp.md), [Java](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/structures/java.md), [JavaScript](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/structures/js.md), [MATLAB](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/structures/matlab.md), [PHP](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/structures/php.md), [Python](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/structures/python.md), [Ruby](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/structures/ruby.md), [Swift](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/structures/swift.md).

## Struct Syntax

A structure groups one or more named fields, of any type, into a new type. For example:

```slice
module M
{
    struct TimeOfDay
    {
        short hour;         // 0 - 23
        short minute;       // 0 - 59
        short second;       // 0 - 59
    }
}
```

This definition introduces a new type named `TimeOfDay`. The name of a field must be unique within its structure.

A structure contains only fields: as elsewhere in Slice, type definitions cannot be nested, except for [modules](https://docs.zeroc.com/ice/3.8/slice/modules/cpp.md). Like every other type, you define a structure in a module.

## Language Mapping

Slice structures map to C++ structures with the same name. For each Slice field, the C++ structure contains a public data member. For example, here is our Employee structure once more:

```slice
struct Employee
{
    long number;
    string firstName;
    string lastName;
}
```

The Slice-to-C++ compiler generates the following definitions for this structure:

```cpp
struct Employee
{
    std::int64_t number;
    std::string firstName;
    std::string lastName;

    [[nodiscard]]
    std::tuple<const std::int64_t&, const std::string&, const std::string&> ice_tuple() const;
};

std::ostream& operator<<(std::ostream& os, const Employee& value);
```

For each field in the Slice definition, the C++ structure contains a corresponding public data member of the same name. Constructors are intentionally omitted so that the C++ structure qualifies as a *plain old datatype* (POD).

### Comparison Operators

The generated C++ structures use templated comparison operators included from Ice.

```cpp
// !=, <, <=, >, >=  are implemented in the same manner
template<
    class T,
    std::enable_if_t<
        std::is_member_function_pointer_v<decltype(&T::ice_tuple)> &&
            !std::is_polymorphic_v<T>, bool> = true>
inline bool operator==(const T& lhs, const T& rhs)
{
    return lhs.ice_tuple() == rhs.ice_tuple();
}
```

These operators compare the `std::tuple` returned by the generated `ice_tuple()` function.

### Default Constructor

Structures have a default constructor that default-constructs each data member. This constructor does not initialize a data member of type `bool`, of an integer, floating-point or enumeration type. The Slice compiler generates a data member initializer for each field with a [default value](https://docs.zeroc.com/ice/3.8/slice/fields/cpp.md#default-values).

### Printing Structs

The Slice compiler generates an `operator<<` that prints the C++ structure, including the value of all its data members:

```cpp
std::ostream& operator<<(std::ostream& os, const Employee& value);
```

You can suppress the generation of this operator, and tell the Slice compiler you’ll provide your own custom operator\<\<, with the `"cpp:custom-print"` metadata. For example:

```slice
// We'll provide our own custom operator<< for this struct.
["cpp:custom-print"]
struct Employee
{
    long number;
    string firstName;
    string lastName;
}
```

## See Also

- [Fields](https://docs.zeroc.com/ice/3.8/slice/fields/cpp.md)
- [Classes](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/cpp.md)
