2
#include <string_view>

class str_ref : public std::string_view
{
public:
  using std::string_view::string_view;
};

int main()
{
  std::string_view sv;
  str_ref sr("", 0);
  str_ref sr2(sv); // error C2664: 'str_ref::str_ref(const str_ref &)': cannot convert argument 1 from 'std::string_view' to 'const char *const '
}

Why is the constructor for (string_view) not being found here? Shouldn't this constructor be imported with the using statement? The (const char*, size_t) constructor is being found. I'm using VS2017.

XTF
  • 1,091
  • 1
  • 13
  • 31

1 Answers1

0

Shouldn't this constructor be imported with the using statement?

It is correctly imported, but you have to define that constuctor in the derived class yourself.
The compiler doesn't automatically generate a constructor like that for your derived class:

#include <string_view>

class str_ref : public std::string_view
{
public:
  using std::string_view::string_view;
  str_ref(const std::string_view& sv) : std::string_view(sv) {} // <<<<
};

int main()
{
  std::string_view sv;
  str_ref sr("", 0);
  str_ref sr2(sv);
}

See Live Demo

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Excuse me, but what's the point of posting a duplicate answer (yes, I see the code) to an obviously duplicate question? You can close duplicates, right? – iehrlich May 26 '17 at 12:11
  • @iehrlich I didn't spot the dupe when I was writing my answer. Also having duplicates for upvoted questions isn't inherently bad. These may serve as signposts for future research. – πάντα ῥεῖ May 26 '17 at 12:13
  • Sure, no problem :) – iehrlich May 26 '17 at 12:30