Elegant function definition in C++

McD0n3ld

I have the following functions:

void func1(int8_t input);
void func1(int16_t input);
void func1(int32_t input);
void func1(float input);

The implementation of the four functions is the same, except for some internal value that are in accordance with the input type (int8_t, int16_t, int32_t or float).

Currently I am creating the other three functions by copy-paste but this is quite tedious.

Is there any way so that I only write 1 function (for example, func1(int8_t input);) and that at building time, the other three functions are created automatically?

Or in which this could work with no need of the other three functions? Any other solution is well appreciated.

LmTinyToon

Usage of template function is the solution for this problem:

template <typename T>
void func1(const T value)
{
   //   Do some stuff
}

If you need to use special internal value, which depends on input type, you can use auxiliary type traits like this:

template <typename T>
struct SomeTrait
{
   static const int value = 10;
};

template <>
struct SomeTrait<float>
{
   static const int value = 20;
};

template <typename T>
void func1(const T value)
{
   std::cout << SomeTrait<T>::value << std::endl;
   //   Do some stuff
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Elegant function definition in C++

From Dev

Definition of a function in a C program

From Dev

C function definition/prototype

From Dev

Function definition in C struct?

From Dev

C function definition and declaration

From Dev

Making this function more "elegant" C++

From Dev

function definition in BNF C grammar

From Dev

c, function definition following struct

From Dev

Find function definition in C++

From Dev

Weird declaration and definition of a function in C

From Dev

Find function definition in C++

From Dev

function definition in BNF C grammar

From Dev

C - Mismatched function prototype and definition for "static" function

From Dev

Is the 'main' function classified as a function definition in C?

From Dev

More elegant way to pass two structs to a callback function in C

From Dev

Elegant way for exiting a function neatly without using goto in C

From Dev

Mixing pointers and references in function definition in C++

From Dev

What encompasses a function definition in C++?

From Dev

Order of definition for function signatures in C++

From Dev

C using a forward declaration within a function definition

From Dev

Preprocessor macro overriding function definition in c++

From Dev

Are these two styles of C function pointer definition different?

From Dev

C++ in function definition of abstract classes

From Dev

Function declaration vs. definition C

From Dev

C++ Member Function Pointer Definition

From Dev

Preprocessor macro definitions used with C function definition

From Dev

Understanding the following C++ function definition

From Dev

C++ in function definition of abstract classes

From Dev

Keyword enum starting a function definition in C++

Related Related

HotTag

Archive