Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type id of std::string for variable vs. string in argument?

I referred to http://en.cppreference.com/w/cpp/language/typeid to write code which does different things for different types.

The code is as below and the explanation is given in the comments.

#include <iostream>
#include <typeinfo>

using namespace std;

template <typename T>
void test_template(const T &t)
{
    if (typeid(t) == typeid(double))
        cout <<"double\n";
    if (typeid(t) == typeid(string))
        cout <<"string\n";
    if (typeid(t) == typeid(int))
        cout <<"int\n";
}

int main()
{
    auto a = -1;
    string str = "ok";
    test_template(a); // Prints int
    test_template("Helloworld"); // Does not print string
    test_template(str); // Prints string
    test_template(10.00); // Prints double

    return 0;
}

Why does test_template(str) print "string" whereas test_template("Helloworld") does not?

BTW, my g++ version is g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609.

like image 601
Meehatpa Avatar asked Jan 09 '17 14:01

Meehatpa


People also ask

Is std::string the same as string?

There is no functionality difference between string and std::string because they're the same type. That said, there are times where you would prefer std::string over string .

What type is std::string?

The std::string type is the main string datatype in standard C++ since 1998, but it was not always part of C++. From C, C++ inherited the convention of using null-terminated strings that are handled by a pointer to their first element, and a library of functions that manipulate such strings.

How do you determine the datatype of a variable in C++?

To get the datatype of variable, use typeid(x). name() of typeinfo library. It returns the type name of the variable as a string.

What does std::string () do?

std::string class in C++ C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. String class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.


2 Answers

In this call

test_template("Helloworld"); // Does not print string

the argument "Helloworld" is a string literal that has type const char[11].

Because the function parameter is a referenced type

void test_template(const T &t) 
                          ^^^

then within the function the argument (more precisely the parameter) has the type const char ( &t )[11].

String literals in C++ have types of constant character arrays with the number of elements equal to the number of characters in string literal plus the terminating zero.

In this call

test_template(str); 

the argument has type std::string because the variable str is declared like

string str = "ok";
^^^^^^

It was initialized by the string literal "ok" nevertheless the object itself is of the type std::string.

like image 87
Vlad from Moscow Avatar answered Sep 21 '22 17:09

Vlad from Moscow


String literals in C++ are of type const char[N+1], where N is the number of characters in the string. std::string is a standard library class which owns a string and provides a number of operations over it. A std::string can be constructed from a const char[N], but they are not the same thing.

like image 28
TartanLlama Avatar answered Sep 23 '22 17:09

TartanLlama