3

I am trying to make a fold expression in a function which populates the outgoing parameters of a function with some values that come in from a string vector. My fold expression is like:

  ((if constexpr (std::is_integral_v<Args>)
  {
      args = std::stoi(vec[index++]);
  }
  else if constexpr (std::is_same_v<Args, std::string>)
  {
      args = vec[index++];
  }
  else
  {
      throw std::invalid_argument("Unsupported argument type.");
  }), ...);

but it fails to compile with a strange error message:

clang: error: expected expression

or

gcc: error: expected primary-expression before 'if'

(as seen on https://gcc.godbolt.org/z/xeq3j6oE7)

Does anyone have a hint on how to properly fix this?

Edit

Full context for the problem is this short application:

#include <vector>
#include <string>
#include <type_traits>
#include <iostream>
#include <stdexcept>
template <typename... Args>
void populateArgs(std::vector<std::string>& vec, Args&... args)
{
    const size_t numArgs = sizeof...(Args);
    if (vec.size() != numArgs)
    {
        throw std::invalid_argument("Number of arguments doesn't match the size of the vector.");
    }
    int index = 0;
    ((if constexpr (std::is_integral_v<Args>)
      {
          args = std::stoi(vec[index++]);
      }
      else if constexpr (std::is_same_v<Args, std::string>)
      {
          args = vec[index++];
      }
      else
      {
          throw std::invalid_argument("Unsupported argument type.");
      }), ...);
}

int main()
{
    std::vector<std::string> vec{ "1", "2", "3", "hello" };
    short a;
    int b;
    long long c;
    std::string d;
    populateArgs(vec, a, b, c, d);
    std::cout << "a = " << a << ", b = " << b << ", c = " << c << ", d = " << d << std::endl;
    // Output: a = 1, b = 2, c = 3, d = hello
}
Aamir
  • 1,974
  • 1
  • 14
  • 18
Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167

2 Answers2

6

Like this:

([&]{
    // if constexpr ...
}(), ...);

This creates a lambda and immediately calls it.


I remember this causing some problems on MSVC. If it doesn't work for you, you can try:

([&]<typename T>()
{
    // Use `T` instead of `Args` here.
}.operator()<Args>(), ...);

, or a separate template function.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
1

if constexpr imho is not a great way of doing a "switch" on types. Imho you're better off creating overloads of a function template instead.

Furthermore I strongly recommend causing a compiler error to indicate invalid parameters being passed to populateArgs.

// set to 0 to use exception instead
#define DETECT_ERROR_AT_COMPILETIME 1

// set to 1 to simulate an invalid use of populateArgs 
#define USE_INCORRECT_CALL 0

namespace impl // use a suitable namespace in case the functionality becomes part of a header file
{
template<class T>
constexpr bool AlwaysFalse = false;

template<class T>
void AssignFromConvertedString(T& lhs, std::string const& str)
{
#if DETECT_ERROR_AT_COMPILETIME
    static_assert(AlwaysFalse<T>, "Unsupported argument type.");
#else
    throw std::invalid_argument("Unsupported argument type.");
#endif
}

template<std::integral T>
void AssignFromConvertedString(T& lhs, std::string const& value)
{
    lhs = std::stoi(value);
}

inline void AssignFromConvertedString(std::string& lhs, std::string const& value)
{
    lhs = value;
}

}

template <typename... Args>
void populateArgs(std::vector<std::string>& vec, Args&... args)
{
    const size_t numArgs = sizeof...(Args);
    if (vec.size() != numArgs)
    {
        throw std::invalid_argument("Number of arguments doesn't match the size of the vector.");
    }
    size_t index = 0;
    ((impl::AssignFromConvertedString(args, vec[index++])), ...);
}

int main()
{
    std::vector<std::string> vec{ "1", "2", "3", "hello" };
    short a;
    int b;
#if USE_INCORRECT_CALL
    std::nullptr_t c;
#else
    long long c;
#endif
    std::string d;
    populateArgs(vec, a, b, c, d);
    std::cout << "a = " << a << ", b = " << b << ", c = " << c << ", d = " << d << std::endl;
    // Output: a = 1, b = 2, c = 3, d = hello
}

Code on godbolt

fabian
  • 80,457
  • 12
  • 86
  • 114