【发布时间】:2018-04-03 10:27:24
【问题描述】:
我有以下功能:
/// <summary>
/// Check whether next character in std::basic_istream is what expected. Skip it if so; otherwise, set fail flag.
/// </summary>
template <typename TCharType, TCharType char_>
std::basic_istream<TCharType>& skipIf(std::basic_istream<TCharType>& istream_)
{
if ((istream_ >> std::ws).peek() == char_) {
istream_.ignore();
}
else {
istream_.setstate(std::ios_base::failbit);
}
return istream_;
}
它是这样工作的:
std::istringstream is {"some ; string"};
std::string temp;
if(is >> temp >> skipIf<char, ';'> >> temp) {
// blah blah
}
有没有办法从给定的char_ 模板参数中推断出TCharType?如果我能写就更好了
-
skipIf<';'>-> 推导出为char -
skipIf<L';'>-> 推导出为wchar_t -
skipIf<u';'>-> 推导出为char16_t -
skipIf<U';'>-> 推导出为char32_t
【问题讨论】:
-
C++17 是一个选项?
-
是的,绝对的。
标签: c++ templates c++17 type-deduction