FCF 2.0 development in progress...
> > > >
[News] [C++ Libraries API] [C++ Downloads] [Donate to the project] [Contacts]

fcf::convert() function

template <typename TResult, typename TSource>
TResult fcf::convert(const TSource& a_source, fcf::Error& a_error)
template <typename TResult, typename TSource>
TResult fcf::convert(const TSource& a_source, fcf::Error&& a_error = fcf::Error{true})
template <typename TResult, typename TSource>
TResult fcf::convert(const TSource& a_source, fcf::ConvertOptions& a_options, fcf::Error& a_error)
template <typename TResult, typename TSource>
TResult fcf::convert(const TSource& a_source, fcf::ConvertOptions& a_options, fcf::Error&& a_error = fcf::Error{true})
template <typename TDest, typename TSource>
void fcf::convert(TDest& a_dst, const TSource& a_source, fcf::Error& a_error)
template <typename TDest, typename TSource>
void fcf::convert(TDest& a_dst, const TSource& a_source, fcf::Error&& a_error = fcf::Error{true})
template <typename TDest, typename TSource>
void fcf::convert(TDest& a_dst, const TSource& a_source, fcf::ConvertOptions& a_options, fcf::Error& a_error)
template <typename TDest, typename TSource>
void fcf::convert(TDest& a_dst, const TSource& a_source, fcf::ConvertOptions& a_options, fcf::Error&& a_error = fcf::Error{true})

Package: fcfBasis

File: bits/PartConvert/convert.hpp ( link_to_the_declaration_in_the_file )

Available from version: 1.0.1

Performs type conversion between two types, either by returning a new instance or by populating an existing destination object.

The convert function is the primary high-level interface for performing type conversions within the framework. It is highly flexible and supports two distinct operational modes:

  • Value Mode (Return by Value): The function creates and returns a new instance of the desired destination type. This is used when you want to obtain a converted value without modifying any existing objects.

    Syntax:

    fcf::convert<TResult>(source, error)
    .
  • Reference Mode (Pass by Reference): The function populates a pre-existing destination object provided as an argument. This is more efficient when the destination object is large or when you want to reuse memory.

    Syntax:

    fcf::convert(destination, source, options, error)
    .

The conversion process is driven by the fcf::ConvertSelector, which searches for the most appropriate fcf::Converter based on the requested fcf::EConverterLevel. This ensures that conversions are performed using the most direct and optimized path available, ranging from simple C-style casts to complex string parsing or custom logic.

Additionally, the fcf::ConvertOptions structure can be used to pass specific configuration parameters to the conversion logic

The fcf::Error object provides detailed feedback if the conversion fails. If this object is not provided, or if it is passed with the fcf::Error::loud flag set, an exception is thrown in the event of an error.

Template arguments
TResult - Return type
TDest - Conversion receiver type between types
TSource - Source data type
Result
TResult
- A new instance of the requested destination type.

Example: Demonstrating Value and Reference conversion modes

An example showing how to convert a string to an integer using Value Mode (returning a new object) and how to convert an integer to a string using Reference Mode (populating an existing object).

#define FCF_BASIS_IMPLEMENTATION #include <fcfBasis/basis.hpp> #include <iostream> #include <string> int main() { // 1. Initialize error fcf::Error err; // 2. Value Mode Example: Converting a string to an integer // We must specify the destination type (int) as a template argument. std::string input_str = "12345"; int val = fcf::convert<int>(input_str, err); if (!err) { std::cout << "[Value Mode] Converted integer: " << val << std::endl; } else { std::cout << "[Value Mode] Error: " << err.error << std::endl; } // 3. Reference Mode Example: Converting an integer to a string // We pass an existing string object as the first argument. int num = 6789; std::string output_str; try { fcf::convert(output_str, num); std::cout << "[Reference Mode] Converted string: " << output_str << std::endl; } catch(const fcf::Exception& e){ std::cout << "[Reference Mode] Error: " << e.what() << std::endl; } return 0; }

Output:

[Value Mode] Converted integer: 12345 [Reference Mode] Converted string: 6789

The convert function provides a unified and type-safe way to handle transformations between different data types, seamlessly switching between returning new values and updating existing ones.