C++ 中 std::forward 的多个参数

Multiple argument with std::forward in C++

我有一个有 5 个成员的结构,我用 std::forward 初始化所有参数:

#include <iostream>
#include <string>
#include <utility>
struct OutputFilesName {
  template<typename T>
  OutputFilesName(
    T&& result_subdir_name,
    T&& result_TSIM_minus, 
    T&& result_TSIM_plus,
    T&& result_minus_total_dat, 
    T&& result_plus_total_dat,
    T&& result_hdl 
    ) : 
      subdir_name( std::forward<T>(result_subdir_name) ),
      TSIM_minus( std::forward<T>(result_TSIM_minus) ),
      TSIM_plus( std::forward<T>(result_TSIM_plus) ),
      minus_total_dat( std::forward<T>(result_minus_total_dat) ),
      plus_total_dat( std::forward<T>(result_plus_total_dat) ),
      hdl( std::forward<T>(result_hdl) ) {     
  }

  const std::string subdir_name;
  const std::string TSIM_minus;
  const std::string TSIM_plus;
  const std::string minus_total_dat;
  const std::string plus_total_dat;
  const std::string hdl;

  OutputFilesName() = delete;
};

我在主程序中调用该结构:

int main(){    
  OutputFilesName ofn(
    "a",
    "bb",
    "ccc",
    "dddd",
    "eeeee",
    "ffffff"
  );
}

编译器报错,似乎参数无法正确解析:

./test_genHoughMap.cc:37:19: error: no matching constructor for initialization of 'OutputFilesName'
  OutputFilesName ofn(
                  ^
./test_genHoughMap.cc:10:3: note: candidate template ignored: deduced conflicting types for parameter 'T'
      ('char const (&)[2]' vs. 'char const (&)[3]')
  OutputFilesName(
  ^
./test_genHoughMap.cc:8:8: note: candidate constructor (the implicit copy constructor) not viable: requires
      1 argument, but 6 were provided
struct OutputFilesName {
       ^
./test_genHoughMap.cc:8:8: note: candidate constructor (the implicit move constructor) not viable: requires
      1 argument, but 6 were provided
./test_genHoughMap.cc:33:3: note: candidate constructor not viable: requires 0 arguments, but 6 were
      provided
  OutputFilesName() = delete;
  ^
1 error generated.

我该如何解决?

问题是您的构造函数对所有参数使用通用类型 T。 现在对于第一个参数 char[2] 推导出第二个 char[3] 有什么冲突。如果你传递所有长度相同的字符串it compiles

因此对于每个参数,您需要不同的模板参数 (demo)。

无论如何考虑放弃使用显式构造函数并只使用 aggregate initialization (demo)。遗憾的是,这在 emplace_back.

这样的情况下不起作用

参考Marek R的回答,

我也提供了另一种解决方案,

在这种情况下可以允许输入参数的类型是char[]

#include <iostream>
#include <string>
#include <utility>

struct OutputFilesName {
  template<typename T1,typename T2,typename T3,typename T4,typename T5,typename T6>
  OutputFilesName(
    T1&& result_subdir_name,
    T2&& result_TSIM_minus, 
    T3&& result_TSIM_plus,
    T4&& result_minus_total_dat, 
    T5&& result_plus_total_dat,
    T6&& result_hdl 
    ) : 
      subdir_name( std::forward<T1>(result_subdir_name) ),
      TSIM_minus( std::forward<T2>(result_TSIM_minus) ),
      TSIM_plus( std::forward<T3>(result_TSIM_plus) ),
      minus_total_dat( std::forward<T4>(result_minus_total_dat) ),
      plus_total_dat( std::forward<T5>(result_plus_total_dat) ),
      hdl( std::forward<T6>(result_hdl) ) {     
  }

  const std::string subdir_name;
  const std::string TSIM_minus;
  const std::string TSIM_plus;
  const std::string minus_total_dat;
  const std::string plus_total_dat;
  const std::string hdl;

  OutputFilesName() = delete;
};

int main(){    
  OutputFilesName ofn(
    "a",
    "bb",
    "ccc",
    "dddd",
    "eeeee",
    "ffffff"
  );
}

我把<typename T>改成了

template<typename T1,typename T2,typename T3,typename T4,typename T5,typename T6>

每个模板类型被独立地推断为其类型。