【问题标题】:How to init unknown string type?如何初始化未知字符串类型?
【发布时间】:2020-11-24 02:48:58
【问题描述】:

假设我有这样的课程

template<typename CharT>
class basic_class
{
  public:
    using char_type   = CharT;
    using string_type = std::basic_string<CharT>;
  private:
    const char_type   ch = '?';
    const string_type str{"How to init"};
};

如果是char也可以,wchar_t不行。

  1. 我该怎么做?

编辑:

我决定编写一个函数,将表单 char 和 string 转换为各种类型,比如

template<typename To>
constexpr To to_type(char val)
{}

template<>
constexpr char     to_type<char>(char val)
{
   return val;
}

template<>
constexpr wchar_t  to_type<wchar_t>(char val)
{
   //....
}

template<>
constexpr char16_t to_type<char16_t>(char val)
{
   //....
}

template<>
constexpr char32_t to_type<char32_t>(char val)
{
   //....
}

template<typename To>
constexpr To             to_type(std::string val)
{}

template<>
constexpr std::string    to_type<std::string>(std::string val)
{
   return val;
}

template<>
constexpr std::wstring   to_type<std::wstring>(std::string val)
{
   //....
}

template<>
constexpr std::u16string to_type<std::u16string>(std::string val)
{
   //....
}

template<>
constexpr std::u32string to_type<std::u32string>(std::string val)
{
   //....
}

那我就这样用吧

template<typename CharT>
class basic_class
{
  public:
    using char_type   = CharT;
    using string_type = std::basic_string<CharT>;
  private:
    const char_type   ch = to_type<char_type>( '?' );
    const string_type str{ to_type<string_type>( "How to init" ) };
};

在 if 语句中

if ( ch  == to_type<char_type>('?') )
{
  //....
}

if ( str == to_type<string_type>("How to init") )
{
  //....
}
  1. 如何将char和std::string转换成其他类型?

欢迎所有建议,提前致谢。

【问题讨论】:

  • 是否必须将其初始化为特定的硬编码字符串?可以将字符串传递给basic_class 构造函数吗?
  • 感谢您的回复,但我知道我只想初始化它的字符串的值。
  • 或许你可以通过专业化来解决?但请记住,“字符”类型比 charwchar_t 更多。
  • 但是在 if 语句中我还有更多的字符串要使用吗?我应该所有都是成本,这是个好主意吗?
  • @某程序员老兄你能看到更新问题吗?

标签: c++ string class templates


【解决方案1】:

你可以定义一个返回正确类型值的函数:

template<typename CharT>
class basic_class
{
  public:
    using char_type   = CharT;
    using string_type = std::basic_string<CharT>;
  private:
    constexpr auto getInitString() {
        if constexpr (is_same<CharT, wchar_t>::value) {
            return L"How to init";
        }
        else {
            return "How to init";
        }
    }

    const char_type   ch = '?';
    const string_type str{getInitString()};
};

这适用于 C++17,但在早期版本上会有点棘手。

另一种方法是从某些东西中复制:

std::string_view initString = "How to init"sv;

template<typename CharT>
class basic_class
{
  public:
    using char_type   = CharT;
    using string_type = std::basic_string<CharT>;
  private:
    const char_type   ch = '?';
    const string_type str{initString.begin(), initString.end()};
};

【讨论】:

  • 感谢您的回答,但是在 if 语句中我还有更多的字符串要使用吗?我应该所有都是成本,这是个好主意吗?
  • 你可以在else if constexpr ()表达式中使用其他类型的字符串
  • 我可以有一个字符类并将其提供给该类,这是个好主意吗?
  • 这取决于您要解决的问题。
  • @Srilakshmikanthan,首先,要回答您的问题,我需要查看功能的实现。你想转换成什么类型​​?从char* 转换和从wchar_t* 转换有什么区别?接下来,您可能会重新发明轮子,应该只使用std::from_chars
猜你喜欢
  • 2011-01-14
  • 2019-06-08
  • 2023-03-21
  • 2013-10-04
  • 1970-01-01
  • 2020-09-21
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
相关资源
最近更新 更多