Search code examples
c++c++11type-conversionunderlyingtype

Finding a type (maybe underlying) from a structure type for the usage in templates


Maybe it is already answered or common thing, but I am missing some proper term for searching about it.

For the below sample code:

// In a separate file
enum class SignsEnum : uint32_t
{
    S1 = 0,
    S2,
    S3
};

std::array<SignsEnum, 10> arrayMyEnum1 = {{.....}};


// other template class file
template<typename ENUM_T, typename ARRAY_T>
class SignsProc
{

    int32_t SignConvrsn(ENUM_T InSign)
    {
    }

    int32_t ProcData(ARRAY_T& InData)
    {}

}

The template class is going to be instantiated with template arguments be from the above example enum and array types:

e.g. SignsProc<SignsEnum , std::array<SignsEnum, 10>> objSignsProc;

In actual, I might have to pass maybe 4-5 more types to the template arguments of the class. Whereas, e.g. in the above example, array holds the object of type "SignsEnum" as array elements. Now, this type is again being passed as 1st template argument, so that function (SignConvrsn) could be defined as in the above example. So, is it possible to find somehow and use the type "SignsEnum" from the array type instead having to pass it separately?

Problem is: Too many template arguments (and seems redundant)

Aim is: to reduce the number of template arguments

Thank you

Edit (additional scenario):

if, for the case, array element is a structure and noe of the structure element has the type SignsEnum, then would it be possible to extract "SignsEnum" type from it?

// In a separate file
enum class SignsEnum : uint32_t
{
    S1 = 0,
    S2,
    S3
};

struct SignsConfig
{
    SignsEnum sign;
    int32_t config1;
    int32_t config2;
}

std::array<SignsConfig, 10> arrayMyEnum1 = {{.....}};

Solution

  • You have two options to reduce the number of arguments:

    1. Option 1. Pass array template argument and extract the element type with ARRAY::value_type
    2. Option 2. If the container is always going to be an array of a certain size, pass the element type and construct array type from it.