Challenge: Type-Based Addition and Type Information

Apply the knowledge you gained in this section to solve the type-based addition and type information challenge.

We'll cover the following

Problem statement

Handling operations on similar types in a type-safe manner is critical for building robust applications. You’re tasked to implement a function called type_based_addition that performs addition based on the types of its arguments. The function should be able to handle a variety of input types, including integers, floating-point numbers, strings, etc. Additionally, implement a function get_type_info that takes a variable and tells the type of that variable. To achieve this, you need to use the knowledge you have gained so far, especially the type traits and conditional compilations.

Note: Create or reuse any auxiliary functions needed to solve the challenge.

Analysis

We can extract the following information from the problem statement:

  • The type_based_addition function is expected to have the following behavior:

    • The function should return the sum if the input arguments are integers, floating-point, or double numbers.

    • The function should concatenate the input arguments if they are strings.

    • If the input arguments are pointers of types convertible to std::string, then the function should concatenate the dereferenced pointers.

  • The get_type_info takes variables of any type and prints their types, values, and the output of their sum on the console.

Let’s use the illustration below to exemplify the required functionality.

Get hands-on with 1200+ tech skills courses.