Home > Enterprise >  Variadic Templates: how to "look ahead" in the arguments
Variadic Templates: how to "look ahead" in the arguments

Time:02-25

I am implementing a printf version which can also handle std::string arguments. At the heart of it there are these functions:

// use sprintf to transform the single format string with the value t
template<typename T>
std::string simpleFormat(const std::string sFormat, const T t) {

    size_t required = snprintf(NULL, 0, sFormat.c_str(), t);
    char sTemp[required 1];
    sprintf(sTemp, sFormat.c_str(), t);
    return std::string(sTemp);
} 

// termination of recursion
std::string recursiveFormat(stringvec &vParts, stringvec &vFormats, uint i) {
    return vParts[i];
}

// recursively walk through arguments 
template<typename T, typename... Args>
std::string recursiveFormat(stringvec &vParts, stringvec &vFormats, uint i, T value, Args... args) {

    std::string sRes = "";
    if (i < vFormats.size()) {
        sRes  = vParts[i];
        sRes  = simpleFormat(vFormats[i], value);
        sRes  = recursiveFormat(vParts, vFormats, i 1, args...);
    }
    return sRes;
}

This works very well. However, to handle a format strings with an asterisk, like for instance in printf("%0*d", width, value) this approach doesn't work.

I tried by adding two functions

// get the next argument and return it
template<typename U, typename... Args>
U fetchNextParam(const U value2, Args... args) {
    return value2;
}

// handle the star by providing sprintf with two values
template<typename T, typename U>
std::string starFormat(const std::string sFormat, const T value1, const U value2) {

    size_t required = snprintf(NULL, 0, sFormat.c_str(), value1, value2);
    char sTemp[required 1];
    sprintf(sTemp, sFormat.c_str(), value1, value2);
    return std::string(sTemp);
} 

and modify the recursiveFormat() function like this:

template<typename T, typename... Args>
std::string recursiveFormat(stringvec &vParts, stringvec &vFormats, uint i, T value, Args... args) {

    std::string sRes = "";
    if (i < vFormats.size()) {
        sRes  = vParts[i];
        //--- trying to handle star
  
        if (vFormats[i].find('*') != std::string::npos) {
            auto value2 = fetchNextParam(args...);
            sRes  = starFormat(vFormats[i], value, value2);
            
        } else {
            sRes  = simpleFormat(vFormats[i], value);
        } 
        sRes  = recursiveFormat(vParts, vFormats, i 1, args...);
    }
    return sRes;
}

However, this code won't compile: i get the compiler message:

sptest.cpp: In instantiation of ‘std::string recursiveFormat(stringvec&, stringvec&, uint, T, Args ...) [with T = double; Args = {}; std::string = std::__cxx11::basic_string<char>; stringvec = std::vector<std::__cxx11::basic_string<char> >; uint = unsigned int]’:
sptest.cpp:114:32:   recursively required from ‘std::string recursiveFormat(stringvec&, stringvec&, uint, T, Args ...) [with T = int; Args = {double}; std::string = std::__cxx11::basic_string<char>; stringvec = std::vector<std::__cxx11::basic_string<char> >; uint = unsigned int]’
sptest.cpp:114:32:   required from ‘std::string recursiveFormat(stringvec&, stringvec&, uint, T, Args ...) [with T = int; Args = {int, double}; std::string = std::__cxx11::basic_string<char>; stringvec = std::vector<std::__cxx11::basic_string<char> >; uint = unsigned int]’
sptest.cpp:124:36:   required from here
sptest.cpp:107:41: error: no matching function for call to ‘fetchNextParam()’
  107 |             auto value2 = fetchNextParam(args...);
      |                           ~~~~~~~~~~~~~~^~~~~~~~~
sptest.cpp:58:3: note: candidate: ‘template<class U, class ... Args> U fetchNextParam(U, Args ...)’
   58 | U fetchNextParam(const U value2, Args... args) {
      |   ^~~~~~~~~~~~~~
sptest.cpp:58:3: note:   template argument deduction/substitution failed:
sptest.cpp:107:41: note:   candidate expects at least 1 argument, 0 provided
  107 |             auto value2 = fetchNextParam(args...);
      |                           ~~~~~~~~~~~~~~^~~~~~~~~

What must i do to get the functionality to look "one ahead" in the variadic arguments?

CodePudding user response:

You might use if constexpr (C 17) to activate code only if there is enough parameter:

if (vFormats[i].find('*') != std::string::npos) {
    if constexpr (sizeof...(Args) > 0) {
        auto value2 = fetchNextParam(args...); // std::get<0>(std::tie(args...))
        sRes  = starFormat(vFormats[i], value, value2);
    } else {
        throw std::runtime_error("Wrong number of argument");
    }
} else {
    sRes  = simpleFormat(vFormats[i], value);
}

CodePudding user response:

You can also provide fetchNextParam(/*empty parameter*/)

// it's type need to return something since you store the value into value2
int fetchNextParam(){throw std::runtime_error("Wrong number of argument");}
  • Related