我试图制作'GenerateCombinations'功能,该功能将占用两个容器,并生成一个组合的容器(例如{" Hello"},{" hello"},{" world,","朋友"}会产生{" Hello world"," Hello Friend"}。我试图使用Sfinae,而在操作员 ((上宣布DECLTYPE来推断返回类型,但是我遇到了很多错误,并且有许多不同的尝试。这是当前版本:
#include <vector>
template<typename T, typename S>
using CombinationResult = decltype(operator+(T(),S()));
template<typename T, typename S>
using CombinationResultContainer = std::vector< CombinationResult<T, S>>;
template<typename T, typename S>
CombinationResultContainer<typename T::value_type,typename S::value_type> generateCombinations(T&& first, S&& second)
{
CombinationResultContainer<typename T::value_type, typename S::value_type> result;
result.reserve(first.size() * second.size());
for (auto& t : first)
for (auto& s : second)
result.push_back(t + s);
return result;
}
void main()
{
std::vector<std::string>v1;
std::vector<std::string>v2;
generateCombinations(v1,v2);
}
目前我遇到错误:C2672'Generatecombinations':找不到匹配的超载函数
C2893无法专业化功能模板'std :: vector> generatecombinations(t&amp;&amp; s&amp;&amp;&amp;('
您在寻找笛卡尔产品吗?尝试范围V3的
view::cartesian_product
您的代码问题是您需要删除T和S的引用尝试
typename std::decay_t<T>::value_type
S
相同或
typename boost::range_value<T>::type
T
和 U
不是您的想法。您可能希望它们俩都是std::vector<std::string>
,但是由于v1
和v2
是LVALUES,这意味着T
和U
都是std::vector<std::string>&
,因为参考倒塌。当您尝试这样做时:
CombinationResultContainer<typename T::value_type, typename S::value_type>
这是行不通的,因为T
和U
不是类型,而是参考。这是沿侧模板参数扣除转发引用的性质。
由于您的功能中的任何内容都不依赖于T
或U
是LVALUE参考的事实,因此我建议您将LVALUE引用代替const(T const&
和U const&
(。这将将T
和U
推断为预期类型。