-2

Premise:

I have a variadic template function that accepts PODs (plain-old data structure) with homogeneous member types.

An acceptable POD may be composed entirely of 4-byte integers XOR 4-byte floats.

Internally, the variadic parameter is coerced to a pointer of the underlying type and utilized as a primitive array.

Problem:

Currently, the template function requires the user to provide some additional information about the formatting of these PODs; such as in the following:

Declaration

template<typename ...U>
void foo(const char *format, U... bars);

Usage

Blah much, wow; // Underlying type is integral.
Bleh such, params; // underlying type is float.
//... 
foo("iffi",much,params,such,wow);

It works, but I would like to bypass the formatting string.

Question:

Is there a way to evaluate the underlying type at compile-time?

Edit: The layout of the structure and the names of its members are not known until compile-time. That is, the user of the library is providing an arbitrary homogeneous POD.

  • please provide a [MCVE]; how do those structs really look like? how many different types such as `Blah`, `Bleh` etc. are there? – m.s. Sep 09 '16 at 08:01
  • The comments specify that the structures are POD with uniform/homogeneous underlying type. The problem specifies that the type is provided by a user and is therefore unknown by the library. – Esmeralda Salamone Sep 09 '16 at 16:41

1 Answers1

0

To bypass string formatting, you may create a trait:

template <typename T /*, typename AlwaysVoidForSFINAE = void*/> struct UnderlyingPodType;

template <> struct UnderlyingPodType<Blah> { using type = int; };
template <> struct UnderlyingPodType<Bleh> { using type = float; };

template<typename ...Ts>
void foo(Ts&&... ts)
{
    using types = std::tuple<UnderlyingPodType<std::decay_t<Ts>>...>;
    // ...
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302