DCMTK Version 3.7.0 OFFIS DICOM Toolkit

Functions
OFvisit – OFvariant

Apply a visitor to an OFvariant object. More...

## Functions
template<typename Result , typename Visitor , typename... Alternatives>
Result | OFvisit (Visitor visitor, OFvariant< Alternatives... > &v)
| Applies the given visitor to the given OFvariant object. More...
template<typename Result , typename Visitor , typename... Alternatives>
Result | OFvisit (Visitor visitor, const OFvariant< Alternatives... > &v)
| Applies the given visitor to the given OFvariant object. More...

## Detailed Description

Apply a visitor to an OFvariant object.

See also
  OFget – Get a pointer to the value stored in an OFvariant holding the selected alternative.

## Function Documentation

## ◆ OFvisit() [1/2]

template<typename Result , typename Visitor , typename... Alternatives>

Result OFvisit | ( | Visitor | visitor,
|  | const OFvariant< Alternatives... > & | v
| ) |  |
| related

Applies the given visitor to the given OFvariant object.

Template Parameters

  Result | the type of the returned value. Pre C++11 compiles do not allow determining the result type automatically in a portable way, therefore, it must be explicitly given by the caller.
  Visitor | the type of the visitor, will be deduced automatically.
  Alternatives | the alternatives the given variant could hold, will be deduced automatically.

Parameters

  visitor | the visitor that will be invoked with the alternative currently being held by the given OFvariant object.
  v | a const reference to an OFvariant object that is going to be visited.

Returns
  Let `CurrentAlternative` be the alternative that v currently holds: the result of `visitor( *OFget<CurrentAlternative>( &v ) )` will be converted to `Result` and then returned.

Precondition
  all possible results must be convertible to `Result`.
  `visitor` must be able to take all possible alternatives.

Note
  If C++11 support is available, the visitor will be forwarded using perfect forwarding. If not, the visitor may be copy constructed at least once, therefore, the visitor needs to be copy constructible when pre C++11 compilers are targeted.

### Usage Example:

struct PowerVisitor
{
template<typename Number>
Number operator()( Number number )
{
return number * number;
}
};
struct PrintVisitor
{
template<typename Number>
void operator()( Number number )
{
COUT << number << OFendl;
}
};
struct AssignVisitor
{
template<typename Number>
void operator()( Number& number )
{
number *= number;
}
};
// ...
const OFvariant<int,float,double> v( 3.14 );
OFvariant<int,float,double> result = OFvisit<OFvariant<int,float,double> >( PowerVisitor(), v );
switch( result.index() )
{
case 0: COUT << "int "; break;
case 1: COUT << "float "; break;
case 2: COUT << "double "; break;
}
OFvisit<void>( PrintVisitor(), result );
COUT << "double " << OFvisit<double>( PowerVisitor(), v ) << OFendl; // OK, every alternative fits inside double
COUT << "int " << OFvisit<int>( PowerVisitor(), v ) << OFendl; // OK, value will be truncated!
COUT << "string " << OFvisit<OFString>( PowerVisitor(), v ) << OFendl; // ERROR!
OFvisit<void>( AssignVisitor(), v ); // ERROR, v is const!
OFvisit<void>( PrintVisitor(), v );
OFvariant
A class template that represents a type-safe union.
Definition: ofvriant.h:417
OFvariant::index
size_t index() const
Get the index of alternative that is currently being held.

Output (if the errors were removed):

    double 9.8596
    double 9.8596
    int 9
    3.14

## ◆ OFvisit() [2/2]

template<typename Result , typename Visitor , typename... Alternatives>

Result OFvisit | ( | Visitor | visitor,
|  | OFvariant< Alternatives... > & | v
| ) |  |
| related

Applies the given visitor to the given OFvariant object.

Template Parameters

  Result | the type of the returned value. Pre C++11 compiles do not allow determining the result type automatically in a portable way, therefore, it must be explicitly given by the caller.
  Visitor | the type of the visitor, will be deduced automatically.
  Alternatives | the alternatives the given variant could hold, will be deduced automatically.

Parameters

  visitor | the visitor that will be invoked with the alternative currently being held by the given OFvariant object.
  v | a reference to an OFvariant object that is going to be visited.

Returns
  Let `CurrentAlternative` be the alternative that v currently holds: the result of `visitor( *OFget<CurrentAlternative>( &v ) )` will be converted to `Result` and then returned.

Precondition
  all possible results must be convertible to `Result`.
  `visitor` must be able to take all possible alternatives.

Note
  If C++11 support is available, the visitor will be forwarded using perfect forwarding. If not, the visitor may be copy constructed at least once, therefore, the visitor needs to be copy constructible when pre C++11 compilers are targeted.

### Usage Example:

struct PowerVisitor
{
template<typename Number>
Number operator()( Number number )
{
return number * number;
}
};
struct PrintVisitor
{
template<typename Number>
void operator()( Number number )
{
COUT << number << OFendl;
}
};
struct AssignVisitor
{
template<typename Number>
void operator()( Number& number )
{
number *= number;
}
};
// ...
OFvariant<int,float,double> v( 3.14 );
OFvariant<int,float,double> result = OFvisit<OFvariant<int,float,double> >( PowerVisitor(), v );
switch( result.index() )
{
case 0: COUT << "int "; break;
case 1: COUT << "float "; break;
case 2: COUT << "double "; break;
}
OFvisit<void>( PrintVisitor(), result );
COUT << "double " << OFvisit<double>( PowerVisitor(), v ) << OFendl; // OK, every alternative fits inside double
COUT << "int " << OFvisit<int>( PowerVisitor(), v ) << OFendl; // OK, value will be truncated!
COUT << "string " << OFvisit<OFString>( PowerVisitor(), v ) << OFendl; // ERROR!
OFvisit<void>( AssignVisitor(), v );
OFvisit<void>( PrintVisitor(), v );

Output (if the error was removed):

    double 9.8596
    double 9.8596
    int 9
    9.8596

Generated on Mon Dec 15 2025 for DCMTK Version 3.7.0 by Doxygen 1.9.1