3 Replies - 193 Views - Last Post: 03 September 2019 - 06:57 PM Rate Topic: -----

#1 Ashok_AKG   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-September 19

conversion from 'std::string_view'to non-scalar type 'std:

Posted 03 September 2019 - 12:42 AM

#include <iostream>
using namespace std;

int main()
{
    std::string_view src = "src";
    std::string dst = src;
    cout<<"dst result : " << dst << "\n";
    return 0;
}



error: conversion from 'std::string_view' {aka 'std::basic_string_view<char>'} to non-scalar type 'std::string' {aka 'std::__cxx11::basic_string<char>'} requested
    9 |     std::string dst = src;


1. Error will be resolved by modifying std::string dst = src.data();
2. Is there any other potential way to resolve this?
3. Why this is not support in string view?

This post has been edited by Skydiver: 03 September 2019 - 06:31 AM
Reason for edit:: Put code (and error) in code tags. Please learn to do this yourself.


Is This A Good Question/Topic? 0
  • +

Replies To: conversion from 'std::string_view'to non-scalar type 'std:

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7056
  • View blog
  • Posts: 23,992
  • Joined: 05-May 12

Re: conversion from 'std::string_view'to non-scalar type 'std:

Posted 03 September 2019 - 10:49 AM

What happens when you break it up into two lines?
std::string dst;
dst = src;



OR tried something like a modern constructor call?
std::string dst{src};



I've never dealt with std::string_view before, but my assumption is that it is like the new trend in C#'s Span<> and Memory<> types and Rust's str (string reference). I'm guessing that there are somekind of semantics that the designers wanted to maintain and so having a copy constructor in the std::string that takes a std::string_view directly would break the semantics.
Was This Post Helpful? 1
  • +
  • -

#3 Ashok_AKG   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-September 19

Re: conversion from 'std::string_view'to non-scalar type 'std:

Posted 03 September 2019 - 06:09 PM

@Skydiver
Thanks for the quick reply.
I understood your point.
I have few more question on this.
1. std::string dst = src; and std::string dst; dst = src; both are quite same.
why during std::string construction assignment is not working, but same after construction assignment is working?
2. To make it this work std::string dst = src; do I need to add explicit conversion in stringview?
3. Or Is this the issue with standard library?
Info:
When do wise versa from string to string view, there is no issue.
 
std::string src = "src";
std::string_view dst = src;
cout<<"dst result : " << dst << "\n";


This post has been edited by Skydiver: 03 September 2019 - 06:58 PM
Reason for edit:: Put code in code tags. Learn to do this yourself.

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7056
  • View blog
  • Posts: 23,992
  • Joined: 05-May 12

Re: conversion from 'std::string_view'to non-scalar type 'std:

Posted 03 September 2019 - 06:57 PM

View PostAshok_AKG, on 03 September 2019 - 09:09 PM, said:

1. std::string dst = src; and std::string dst; dst = src; both are quite same.
why during std::string construction assignment is not working, but same after construction assignment is working?

Unfortunately, in the eyes of the C++ compiler they are different. The first version:
std::string dst = src;


invokes the copy constructor, while the second:
std::string dst;
dst = src;


invokes the default constructor on the first line, and then invokes the operator =() on the second line.

C++ is flexible enough to let you write a class such that a class can define what kind of parameters a copy constructor will take. In this case, it looks like the standard C++ string class was not written to take a standard C++ string_view class.

View PostAshok_AKG, on 03 September 2019 - 09:09 PM, said:

@Skydiver
2. To make it this work std::string dst = src; do I need to add explicit conversion in stringview?

What explicit conversion would you use? I don't see any such conversions in the documentation.

View PostAshok_AKG, on 03 September 2019 - 09:09 PM, said:

3. Or Is this the issue with standard library?
Info:
When do wise versa from string to string view, there is no issue.
std::string src = "src";
std::string_view dst = src;
cout<<"dst result : " << dst << "\n";


That's because the string_view has a copy constructor that supports that. See (2) in the documentation.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1