0

I have a function which looks like below and I am trying to print out the values in QT.

void myfunction(
    std::string&    Service,
    std::string&    Type,
    std::string&    Hostname,
    std::string&    Address,
    UNUM16      Port,
    std::map< std::string, std::string >& TxtList )
{
    qDebug() << "Hostname capacity is : " << Hostname.capacity();
    qDebug() << "Hostname is : " << QString::fromStdString(Hostname);
    qDebug() << "Port is : " << ntohs(Port);

    std::map< std::string, std::string >::iterator iter;
    iter = TxtList.find("FwVersion");

}

Now when I run the function, first debug statement prints the correct value for capacity of string. But the second debug statement does not work and it just prints empty string. And also the when I do the find on map, the application just crashes.

I have also tried QString::fromUtf8(Hostname.c_str()) function and even that did not work

Is there anyway to convert the values and print them in QT?

Dev
  • 1,529
  • 4
  • 24
  • 36
  • Shouldn't be passing `std::string` as a reference usually. It has its own copy constructor. – Qix - MONICA WAS MISTREATED Jul 17 '14 at 15:27
  • 2
    @Qix, Passing it by const reference is common. By non-const reference should hold to the same rules as passing anything else by non-const reference: if you need to change it and propagate those changes. – chris Jul 17 '14 at 15:28
  • @devana, Please post an [MCVE](http://stackoverflow.com/help/mcve) with strings that give the behaviour you describe. – chris Jul 17 '14 at 15:28
  • @chris they're not passing by const reference though. – Qix - MONICA WAS MISTREATED Jul 17 '14 at 15:29
  • @Qix, Yes, but passing `std::string` by value is often not desirable either. My suggestion would be to make them `const std::string &`. – chris Jul 17 '14 at 15:30
  • @chris You should [always pass `std::string` by value](http://stackoverflow.com/a/10232761/510036). – Qix - MONICA WAS MISTREATED Jul 17 '14 at 15:30
  • @Qix, My understanding around that with `std::string` might have been wrong then. Still, it seems like the general consensus there is that unless you actually need a copy (now or later), passing by const reference is better. Passing by value is definitely better than copying it inside the function, though. – chris Jul 17 '14 at 15:34
  • It doesn't copy though. It `std::move`'s. – Qix - MONICA WAS MISTREATED Jul 17 '14 at 15:37
  • @Qix, If it can, yes (but `std::move` doesn't move, it just creates an rvalue reference to enable moving). Consider `std::string s; foo(s);`. Moving from `s` can't happen automatically. If you plan on using `s` after, moving explicitly isn't an option either. – chris Jul 17 '14 at 15:39
  • 1
    @chris The above function is a callback function from a library I am using. I have to use the std::string reference itself as input parameter and I can not change the signature of the function. I have tried without the reference by creating a variable as value type and using QString::fromstdstring works in that case. But not when the string as declared as refernece. – Dev Jul 17 '14 at 15:42
  • @chris I don't think your understanding was incorrect at all. It'd be silly to pass the string by value in this case. NicolBolas' answer states that if the function is going to make a copy anyway, then pass by value. The next highest voted answer is much more explicit about it. Qix is the one who's confused here. Sometimes it feels to me that that Dave Abraham's post caused more trouble than enlighten people :) – Praetorian Jul 17 '14 at 16:38
  • @Praetorian, Thanks for the input. I was going to edit my comment, but ran out of time. There are still problems with both approaches assuming only one function. Const reference is good for my above example and slightly better than moving when you don't need a copy, but as soon as you do need a copy, you have to do that copy inside the function instead of letting the compiler do it through the parameter. Passing by value is good when moves and RVO kick in, but there are just some situations where that can't happen and a const reference is much better. [cont'd] – chris Jul 17 '14 at 16:57
  • [cont'd] I can't spend too much time thinking about it right now, but you can't change the function signature from by value to by reference when the library's already there or you to use. I'm unsure of whether the library changing from by reference to by value would actually break any code, but I'm sure there's *something* that would break. I don't know, maybe there's some way to abstract the change with a universal reference or something. Apart from that if it works, each has its own problems, so it takes guessing whether you will need a copy later. – chris Jul 17 '14 at 17:00

1 Answers1

2

From qt documentation

QString QString::fromStdString ( const std::string & str ) [static]

Returns a copy of the str string. The given string is converted to Unicode using the fromAscii() function. This constructor is only available if Qt is configured with STL compatibility enabled.

You should not use QString::fromUtf8() as std::string are not UTF8 encoded (unless you put that encoded string there somehow)

Community
  • 1
  • 1
Slava
  • 43,454
  • 1
  • 47
  • 90
  • `std::string` allows for UTF8 encoding, sure. ASCII is inherently UTF8 encoded, and UTF8 allows for proper null-terminated strings. Calling `QString::fromUtf8()` with an `std::string` is perfectly okay. – Qix - MONICA WAS MISTREATED Jul 17 '14 at 15:39