1

Say i have a class like this

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"};
};

It is okay if it is char but not for wchar_t.

  1. How can i do this ?

EDIT:

I decided to write a function that converts form char and string to various type,like

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)
{
   //....
}

and

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)
{
   //....
}

Then i will use this like

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" ) };
};

In if statements like

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

if ( str == to_type<string_type>("How to init") )
{
  //....
}
  1. How to convert char and std::string other types ?

All suggestions are welcome, Thanks in advance.

srilakshmikanthanp
  • 2,231
  • 1
  • 8
  • 25

1 Answers1

0

You may define a function that returns the value of correct 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()};
};

That would work on C++17, on earlier versions that is a little trickier.

Another way is to copy from something:

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()};
};
Dmitry Kuzminov
  • 6,180
  • 6
  • 18
  • 40
  • Thanks for your answer, But i have more number of strings to use, in if statements too ? should i have all are cost, it is good idea? – srilakshmikanthanp Aug 04 '20 at 03:08
  • You may use other types of strings in `else if constexpr ()` expression – Dmitry Kuzminov Aug 04 '20 at 03:09
  • can i have class of characters and supply it to that class, it is good idea? – srilakshmikanthanp Aug 04 '20 at 03:52
  • That depends on the problem you are trying to solve. – Dmitry Kuzminov Aug 04 '20 at 03:58
  • @Srilakshmikanthan, first, to answer your question I would need to see the implementation of the functions. What types are you trying to convert to? What's the difference between the conversion from `char*` and the conversion from `wchar_t*`? Next, you probably reinvent the wheel and should just use `std::from_chars`. – Dmitry Kuzminov Aug 04 '20 at 06:48
  • I edited the the question does it understantable ? and i want convert from char and string – srilakshmikanthanp Aug 04 '20 at 08:19
  • What kind of conversion are you trying to implement? If you are fully satisfied with ASCII characters as the source of your initialization strings, you can reuse my second approach (copying sequence of `char`s into a sequence of wider characters). Overall your problem is not explained. – Dmitry Kuzminov Aug 04 '20 at 09:29
  • Yes, i am only want convert ascii characters. can directly init all type of string with string it is valid? – srilakshmikanthanp Aug 04 '20 at 09:32
  • All specializations of `std::basic_string` have a constructor that accepts two iterators: `InputIterator first, InputIterator last`. I'm using this approach in my second example. – Dmitry Kuzminov Aug 04 '20 at 09:36
  • and can i assign char value to wchar_t ? – srilakshmikanthanp Aug 04 '20 at 09:45
  • For sure you can. Why not to try that? – Dmitry Kuzminov Aug 04 '20 at 09:54
  • I am not well in unicode, I belive that can not directly convert char to wchar_t but what i readed https://stackoverflow.com/questions/8032080/how-to-convert-char-to-wchar-t to convert char to wchar_t used function mbstowcs what is this ?. – srilakshmikanthanp Aug 04 '20 at 10:06
  • does this function do bit by bit copy or someths else ? – srilakshmikanthanp Aug 04 '20 at 10:13
  • You need to answer what does the string `"How to init"` contain. If it is just ASCII characters, that is safe to copy each `char` into `wchar_t`. If you have a string that is not ASCII, that depends on the locale -- you need to deal with that. So far we were discussing a simple `"How to init"` string. – Dmitry Kuzminov Aug 04 '20 at 10:31
  • Yes, it is just ascii characters – srilakshmikanthanp Aug 04 '20 at 10:44