0

I'm searching for a substring using string::find in C++. When I defined a string using const auto and used the variable later down, eclipse replaced . with ->.

I found this SO thread which concludes that auto foo = "bar" is deduced to a (const char *) foo = "bar". So eclipse is correct converting . to -> even though I was a bit baffled to begin with. I assumed incorrectly auto would become std::string.

Would there be a downside deducing auto foo = "bar" to std::string instead of const char * ? Increased code size, slower performance?

Community
  • 1
  • 1
kometen
  • 6,536
  • 6
  • 41
  • 51
  • 7
    The type of `"bar"` is `const char[4]`. I don't know why you think this should be automatically converted to a `std::string`. – IInspectable Feb 27 '16 at 22:16
  • I think you are confusing that fact that you can assign a `const char *` or `const char [4]` to a string: `std::string str = "bar";` with the fact that anything between quotes is just a normal const char array. – code_fodder Feb 27 '16 at 22:21
  • 1
    If you want the type to be `std::string` say so. Don't use `auto` here. – Pete Becker Feb 27 '16 at 23:03

3 Answers3

7

Your code could have a million classes that can be constructed implicitly from a const char *. Why should std::string be chosen?

auto simply saves some keyboard typing you if you want a variable with the same type of the expression¹, not if you want to create a different object.

(1) more or less; things as always get somewhat hairy with C++...

6502
  • 112,025
  • 15
  • 165
  • 265
3

Well, likely, you have just answered your own question. std::string takes slightly more space (it has size counter), its creation involves dynamic allocation etc.

The lack of a complex string type may seem an anachronism nowadays, but since C++ is oriented toward a complete replacement of C with its low-level efficiency, it's pretty explainable.

Moreover, std::string is just a library class. you can choose a different string type, e.g. QString or std::experimental::string_view, if your task requires it. BTW, string_view is much more similar to const char[] since it doesn't provide dynamic manipulations at all and can be used in constexpr

user3159253
  • 16,836
  • 3
  • 30
  • 56
  • Thank you. It was indeed like talking to someone and the next day this sudden realization came to me why auto defaults to char*. I appreciate it a lot you all three took the time answering. – kometen Mar 04 '16 at 18:23
2

"Foobar" is a string literal and not a std::string. This is stored as const char[7] in a read only section of your binary.

std::string te type has an implicit conversion from const char * because it has a single argument constructor without it being explicit which is invoked if you write: std::string s = "foobar";. Note that the default argument of allocator is assigned on the constructor.

Using const auto gives you the actual type instead of a converted type. So converting a string literal to std::string actually creates another object that references the literal.

http://en.cppreference.com/w/cpp/language/string_literal http://en.cppreference.com/w/cpp/string/basic_string

Alexander Oh
  • 24,223
  • 14
  • 73
  • 76