C++ 如何初始化未知字符串类型?
比如说我有一节这样的课C++ 如何初始化未知字符串类型?,c++,string,class,templates,C++,String,Class,Templates,比如说我有一节这样的课 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&quo
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_type=图表;
使用string\u type=std::basic\u字符串;
私人:
常量字符类型ch='?';
const string_type str{“How to init”};
};
如果它是char,但不是wchar\t,则可以
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)
{
//....
}
模板
constexpr To_类型(char val)
{}
模板
constexpr char to_type(char val)
{
返回val;
}
模板
constexpr wchar_t to_type(char val)
{
//....
}
模板
constexpr char16_t to_type(char val)
{
//....
}
模板
constexpr char32_t to_type(char val)
{
//....
}
及
模板
constexpr To_类型(std::string val)
{}
模板
constexpr std::string to_type(std::string val)
{
返回val;
}
模板
constexpr std::wstring to_type(std::string val)
{
//....
}
模板
constexpr std::u16string to_type(std::string val)
{
//....
}
模板
constexpr std::u32string to_type(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" ) };
};
模板
基本类
{
公众:
使用char_type=图表;
使用string\u type=std::basic\u字符串;
私人:
const char_type ch=to_type(“?”);
const string_type str{to_type(“How to init”)};
};
在if语句中,例如
if ( ch == to_type<char_type>('?') )
{
//....
}
if ( str == to_type<string_type>("How to init") )
{
//....
}
if(ch==to_类型('?'))
{
//....
}
if(str==to_type(“如何初始化”))
{
//....
}
欢迎所有建议,提前感谢。您可以定义一个返回正确类型值的函数:
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()};
};
模板
基本类
{
公众:
使用char_type=图表;
使用string\u type=std::basic\u字符串;
私人:
constexpr auto getInitString(){
如果constexpr(相同::值){
返回L“如何初始化”;
}
否则{
返回“如何初始化”;
}
}
常量字符类型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()};
};
std::string_view initString=“How to init”sv;
模板
基本类
{
公众:
使用char_type=图表;
使用string\u type=std::basic\u字符串;
私人:
常量字符类型ch='?';
const string_type str{initString.begin(),initString.end()};
};
您可以定义一个返回正确类型值的函数:
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()};
};
模板
基本类
{
公众:
使用char_type=图表;
使用string\u type=std::basic\u字符串;
私人:
constexpr auto getInitString(){
如果constexpr(相同::值){
返回L“如何初始化”;
}
否则{
返回“如何初始化”;
}
}
常量字符类型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()};
};
std::string_view initString=“How to init”sv;
模板
基本类
{
公众:
使用char_type=图表;
使用string\u type=std::basic\u字符串;
私人:
常量字符类型ch='?';
const string_type str{initString.begin(),initString.end()};
};
是否必须将其初始化为特定的硬编码字符串?字符串是否可以传递给basic_class
构造函数?谢谢你的回答,但我知道字符串的值,我只想初始化它。也许你可以通过专门化来解决它?但是请记住,有比char
和wchar\u t
更多的“字符”类型?“我应该拥有所有的成本,这是个好主意吗?”一些程序员哥们你们能看到更新问题吗?你们必须将它初始化为一个特定的硬编码字符串吗?字符串是否可以传递给basic_class
构造函数?谢谢你的回答,但我知道字符串的值,我只想初始化它。也许你可以通过专门化来解决它?但是请记住,有比char
和wchar\u t
更多的“字符”类型?“我应该所有的都是成本,这是个好主意吗?”一些程序员伙计,你们能看到更新问题吗?谢谢你们的回答,但我有更多的字符串要使用,在if语句中也是如此?我是否应该拥有所有的成本,这是个好主意?您可以在中使用其他类型的字符串,否则如果constexpr()
expression我可以拥有字符类并将其提供给该类,这是个好主意吗?这取决于您试图解决的问题。@Srilakshmikanthan,首先,为了回答你的问题,我需要看看这些功能的实现情况。你想转换成什么类型?从char*
转换与从wchar\u t*
转换有什么区别?接下来,您可能会重新发明轮子,只需使用std::from_chars
。谢谢您的回答,但是我还有更多的字符串要使用,在if语句中也是吗?我是否应该拥有所有的成本,这是个好主意?您可以在中使用其他类型的字符串,否则如果constexpr()
expression我可以拥有字符类并将其提供给该类,这是个好主意吗?这取决于您试图解决的问题。@Srilakshmikanthan,首先,为了回答你的问题,我需要看看这些功能的实现情况。你想转换成什么类型?从char*
转换与从wchar\u t*
转换有什么区别?接下来,您可能会重新发明轮子,只需使用std::from_chars
。