简体   繁体   English

错误C2893:无法专门化功能模板C ++

[英]error C2893: Failed to specialize function template C++

I tried to create a string splitting template like the following, I get errors. 我试图创建一个字符串拆分模板,如下所示,但出现错误。

struct splitX
{
  enum empties_t { empties_ok, no_empties };
};

template <typename Container>
Container& split(
  Container&                                 result,
  const typename Container::value_type&      s,
  typename Container::value_type::value_type delimiter,
  splitX::empties_t                           empties = splitX::empties_ok )
{
  result.clear();
  std::istringstream ss( s );
  while (!ss.eof())
  {
      typename Container::value_type field;
      getline( ss, field, delimiter );
      if ((empties == split::no_empties) && field.empty()) continue;
      result.push_back( field );
  }
  return result;
}

This fails with the error from the headline when I'm trying to use it with the vector class like below: 当我尝试将其与如下所示的vector类一起使用时,由于标题错误而失败:

std::getline(myfile,line);
std::vector<std::string> fields;
split(fields,line,' ');

// Test split function
std::cout << line << std::endl;
for(int i = 0; i < fields.size();i++)
  std::cout << fields[i];

Ok, Microsoft Visual C++ 2008 doesn't like your Container::value_type::value_type . 好的,Microsoft Visual C ++ 2008不喜欢您的Container::value_type::value_type

You can use this monstrous construction: 您可以使用这种怪异的构造:

template <typename T>
struct vte /*value_type_extractor*/
{
    typedef typename T::value_type type;
};

template <typename Container>
Container& split(
   Container&                                 result,
   const typename Container::value_type&      s,
   typename vte<typename vte<Container>::type>::type delimiter,
   splitX::empties_t                           empties = splitX::empties_ok )
{
  typedef typename Container::value_type string_type;
  typedef typename string_type::value_type elem_type;
  std::basic_istringstream<elem_type> ss( s );
  ...
}

Or, you can do it in STL style: 或者,您可以采用STL样式:

#inlcude <iterator>

template <typename OutputIterator, typename StringType>
OutputIterator split(
  OutputIterator                  out,
  const StringType&               s,
  typename StringType::value_type delimiter,
  splitX::empties_t               empties = splitX::empties_ok )
{
  typedef typename StringType::value_type elem_type;
  std::basic_istringstream<elem_type> ss( s );
  while (!ss.eof())
  {
      StringType field;
      std::getline( ss, field, delimiter );
      if ((empties == splitX::no_empties) && field.empty()) continue;
      *out++ = field;
  }
  return out;
}

//somewhere in code
{
    ...
    split(std::back_inserter(your_container), line, ' ');
}

Or, you can stop pretending to be a template god and write two overloaded functions: 或者,您可以停止假装自己是模板神,并编写两个重载函数:

template <typename Container>
Container& split(
   Container&              result,
   const std::string&      s,
   char                    delimiter,
   splitX::empties_t       empties = splitX::empties_ok )
{
  std::istringstream ss(s);
  ...
}

template <typename Container>
Container& split(
   Container&              result,
   const std::wstring&     s,
   wchar_t                 delimiter,
   splitX::empties_t       empties = splitX::empties_ok )
{
  std::wistringstream ss(s);
  ...
}

Visually inspecting your code, I didn't really detect anything amiss. 目视检查您的代码,我没有发现任何错误。 I did a cut and paste of your program, putting a main around your testing code and compiled it (stock g++ on FC 15). 我对您的程序进行了剪切和粘贴,将main放在您的测试代码周围并进行了编译(FC 15中有g ++)。

The compiler found a typo in your split function. 编译器在您的split函数中发现了一个错字。 split::no_empties should be splitX::no_empties . split::no_empties应该被splitX::no_empties Also, with warnings enabled, it reported the for loop is comparing a signed type with an unsigned type. 同样,启用警告后,它报告for循环正在比较带符号类型和无符号类型。

After fixing that typo, and the compiler warning, the routine compiled and ran fine. 修正该错字和编译器警告后,该例程已编译并运行良好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++错误C2893:无法专门化功能模板 - c++ error C2893: Failed to specialize function template 错误C2893:无法专门化功能模板 - error C2893: Failed to specialize function template 错误C2893:无法使用CTPL专门化功能模板 - error C2893: Failed to specialize function template with CTPL 错误:'无法专门化功能模板'C2893'std :: invoke' - Error: 'Failed to specialize function template' C2893 'std::invoke' 错误C2893:无法专门化函数模板VC ++ 2012 - Error C2893: Failed to specialize function template VC++ 2012 错误C2893无法专门化函数模板&#39;unknown-type std :: invoke(_Callable &amp;&amp;,_ Types &amp;&amp; ...)&#39; - Error C2893 Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&…)' 错误C2893:无法专门化函数模板&#39;unknown-type std :: invoke(_Callable &amp;&amp;,_ Types &amp;&amp; ...)noexcept( <expr> )” - error C2893: Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&…) noexcept(<expr>)' 错误C2893:无法专门化功能模板&#39;unknown-type std :: less <void> :: operator()(_ Ty1 &amp;&amp;,_ Ty2 &amp;&amp;)const&#39; - error C2893: Failed to specialize function template 'unknown-type std::less<void>::operator ()(_Ty1 &&,_Ty2 &&) const' C ++错误C2893 - C++ error C2893 Visual C ++ 2015 std :: function与C2893 - Visual C++ 2015 std::function vs C2893
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM