0

for example there is such a template

template<class ... Types> void f(Types ... args);

f();       // OK: args contains no arguments
f(1);      // OK: args contains one argument: int
f(2, 1.0); // OK: args contains two arguments: int and double

and I want to do so in it

template<class ... T>
void f2(T... args) {
  // option 1
  // > hello // world // human
  std::cout <<(args / ...);

  // option 2
  // > hello // world
  std::cout <<((args / ...) and -1 args); // ../
}

in the example, option 2 We are concatenating the Hello and world string and at the same time not using the human, since we don't need it yet.

if it is possible of course

f2("hello", "world", "human");

then

get one less argument inside the function to use it inside the function

For this call:

f2("A", "b", 12);

Expected result should be equivalent to this:

std::cout << "A";
std::cout << "b";

// no statement or different action for: std::cout << 12;

and if possible without arrays and recursions if there is no such functionality, then write that it is not there.

1 Answers1

0
template<bool condition, typename T>
void printIf(T&& arg)
{
    if constexpr (condition) std::cout << arg;
}

template<size_t...indexes, class ... T>
void printAllButLastHelper(std::integer_sequence<size_t, indexes...> v, T&&... args) {
    (printIf<sizeof...(args) - 1 != indexes>(args), ...);
}

template<class ... T>
void f2(T&&... args) {
    std::cout << __PRETTY_FUNCTION__ << '\n';
    printAllButLastHelper(
        std::make_integer_sequence<size_t, sizeof...(args)>{}, 
        std::forward<T>(args)...);
    std::cout << '\n';
}

https://godbolt.org/z/3dojcd

Marek R
  • 32,568
  • 6
  • 55
  • 140