C++ - Is string a built-in data type?
Asked Answered
F

10

23

In C++, is string a built-in data type?

Thanks.

Flemings answered 22/3, 2011 at 8:45 Comment(1)
no, my editor doesn't highlight it :-)Necessitous
O
21

What is the definition of built-in that you want to use? Is it built-in the compiler toolset that you have yes, it should. Is it treated specially by the compiler? no, the compiler treats that type as any user defined type. Note that the same can probably be applied to many other languages for which most people will answer yes.

One of the focuses of the C++ committee is keeping the core language to a bare minimum, and provide as much functionality as possible in libraries. That has two intentions: the core language is more stable, libraries can be reimplemented, enhanced... without changing the compiler core. But more importantly, the fact that you do not need special compiler support to handle most of the standard library guarantees that the core language is expressive enough for most uses.

Simpler said in negated form: if the language required special compiler support to implement std::string that would mean that users don't have enough power to express that or a similar concept in the core language.

Outport answered 22/3, 2011 at 8:55 Comment(1)
The standard makes use of the phrase built-in to mean those compiler intrinsics (things not in the library).Anderton
C
12

It's not a primitive -- that is, it's not "built in" the way that int, char, etc are. The closest built-in string-like type is char * or char[], which is the old C way of doing stringy stuff, but even that requires a bunch of library code in order to use productively.

Rather, std::string is a part of the standard library that comes with nearly every modern C++ compiler in existence. You'll need to #include <string> (or include something else that includes it, but really you should include what your code refers to) in order to use it.

Chirrup answered 22/3, 2011 at 8:54 Comment(0)
H
8

If you are talking about std::string then no.

If you are talking about character array, I guess you can treat it as an array of a built in type.

Halpin answered 22/3, 2011 at 8:47 Comment(0)
U
4

No.

Built-in or "primitive" types can be used to create string-life functionality with the built-in type char. This, along with utility functions were what was used in C. In C++, there is still this functionality but a more intuitive way of using strings was added.

The string class is a part of the std namespace and is an instantiation of the basic_string template class. It is defined as

typedef basic_string<char> string;

It is a class with the ability to dynamically resize as needed and has many member functions acting as utilities. It also uses operator overloading so it is more intuitive to use. However, this functionality also means it has an overhead in terms of speed.

Ulrick answered 22/3, 2011 at 9:24 Comment(0)
V
3

Depends on what you mean by built-in, but probably not. std::string is defined by the standard library (and thus the C++ standard) and is very universally supported by different compilers, but it is not a part of the core language like int or char.

Vestiary answered 22/3, 2011 at 8:50 Comment(0)
L
1

It can be built-in, but it doesn't have to be.

The C++ standard library has a documented interface for its components. This can be realized either as library code or as compiler built-ins. The standard doesn't say how it should be implemented.

When you use #include <string> you have the std::string implementation available. This could be either because the compiler implements it directly, or because it links to some library code. We don't know for sure, unless we check each compiler.

None of the known compilers have chosen to make it a built-in type, because they didn't have to. The performance of a pure library implementation was obviously good enough.

Limoli answered 22/3, 2011 at 10:21 Comment(0)
P
0

No, string is a class.

Polymyxin answered 22/3, 2011 at 8:47 Comment(6)
Rather than class, string is library that provides string manipulation methods instead of object to store strings.Mattheus
@Kush: Maybe it is simplified, but even in C++ Standard (open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf) they speak about a string class (Chapter 21, Strings Library)Polymyxin
@Dadam: agreed with the term, but those who are "only" familiar with the term "class" might wonder if they can create object of string class, which may not be possible under C++, so mentioning it as a library of methods will be more relevant in general sense.Mattheus
@Kush: I didn't know that. Can you give me some simple example?Polymyxin
@Dadam: this page shows how string object gets created but the memory to store string is allocated using char* instead of string object itself, and than methods are used separately, instead of accessing using object name, like we do in any OOP language eg; strobj.strrev();. So as per standard OOP norms, string in C++ can't act as a class whose objects store values themselves and can access its methods. Though, in "C++ STL string", one can still use dot . operator to access methods.Mattheus
@Kush: standard OOP terminology is irrelevant to what Dadam is saying - the C++ standard defines what "class" means in the context of C++, and std::string is a class.Amerind
K
0

Definitely not. String is a class from standard library. char *, or char[] are built-in types, but char, int, float, double, void, bool without any additions (as pointers, arrays, sign or size modifiers - unsigned, long etc.) are fundamental types.

Kodok answered 18/4, 2016 at 10:8 Comment(0)
P
-1

No. It's part of standard library.

Protoplasm answered 22/3, 2011 at 8:46 Comment(0)
C
-4

No. There are different imlementations (eg Microsoft Visual C++), but char* is the C++ way of representing strings.

Cursed answered 22/3, 2011 at 8:46 Comment(4)
Not really, not precise. char* is the C way of representing strings, const char[] is the type of string literals, std::string is the standard way to represent strings.Centigram
Different implementations? In fact the std::string-type is standard and has a standard interface, so it /is/ the "C++ way" of representing strings, not char* (which is also not quite right, if you mean string literals you should use const char*).Vignette
This is incorrect. The string class is the C++ way. char* is the C way.Epagoge
"char* is the C++ way of representing strings" wtfPansy

© 2022 - 2024 — McMap. All rights reserved.