vendredi 2 novembre 2018

Creating string class with static information

I want to create string class with some static templated information and identical std::string interface. I am lazy to wrap std::string so I am trying to use public inheritance and constructor inheritance:

#include <string>
using std::string;

template <typename Type = uint16_t>
class MyString : public string {
public:
   using string::string;
   using str_len_size = Type;
};

This code compiles without problem and I cant construct MyString from const char*:

MyString<uint16_t> str{"hello"};//OK

But when I try to construct MyString from std::string or assign std::string to it I have a compile errors:

std::string std_str{"hello"};
MyString<uint16_t> str{std_str};// error: no matching function for call ...

std::string std_str{"hello"};
MyString<uint16_t> str;
str = std_str;// error:  no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘lottery::base::MyString<short unsigned int>&&’

Why cant I constructor and assign MyString from std::string and how can I achieve this?

I use compiler gcc 7.3.1.

Aucun commentaire:

Enregistrer un commentaire