Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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

Code is as below and 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;
}

I am wondering why test_template(str) prints "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

share|improve this question
11  
Why the downvotes? Sure, it's at a basic level, but the question is complete, clear, and OP even includes a compiler version! – TartanLlama 7 hours ago
1  
"Helloworld" is not a std::string. But "Helloworld"s would be. – Jarod42 6 hours ago
up vote 13 down vote accepted

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.

share|improve this answer
1  
Should conat char be const char? (I can't submit an edit with just one change.) – Christophe Strobbe 5 hours ago
    
@ChristopheStrobbe What do you mean? – Vlad from Moscow 5 hours ago
    
Well, conat in conat char ( &t )[11] in your answer. – Christophe Strobbe 4 hours ago
1  
I mean const is a type qualifier. But what is conat? A typo or something else? – Christophe Strobbe 4 hours ago
1  
@ChristopheStrobbe Oh, I am sorry. It is a typo.:) – Vlad from Moscow 4 hours ago

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.

share|improve this answer

String literals like "Helloworld" are constants arrays of characters.

The std::string class have a constructor that can take pointers to string literals, but a string literal is in itself not a std::string object.


As a side-note, using a function like your is considered a code-smell and bad design. Use overloaded functions taking different arguments instead. That will also solve your problem with the strings.

share|improve this answer
1  
Agreed, there's no point in having a template at all here. There's no common functionality. – AndyG 7 hours ago
1  
Thanks! I will consider using overloaded functions instead. – Meehatpa 7 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.