Improper argument deduction???

D

Dave

Hello all,

Please see the question embedded in the code below. Thanks in advance for
your help!

Dave



#include <iostream>

using namespace std;

void foo(const char p[5])
{
// This yields char const * due to decay.
cout << "Non-template: " << typeid(p).name() << endl;
}

template <typename T>
void foo(T *p)
{
// This yields char; WHY NOT char const ????
cout << "Template: " << typeid(T).name() << endl;

// This yields char const *
cout << "Template: " << typeid(T *).name() << endl;
}

int main()
{
// This yields char const [5]. In sync with page 19 of the Standard;
cool...
cout << typeid("test").name() << endl;

foo("test");
foo<>("test");
}
 
R

Rob Williscroft

Dave wrote in in comp.lang.c++:
Please see the question embedded in the code below. Thanks in advance
for your help!


[with T = char const]
// This yields char; WHY NOT char const ????
cout << "Template: " << typeid(T).name() << endl;

Because the type of "char const" is "char", const is just a qualifier.

IOW: char and char const are not distinct types.

HTH.

Rob.
 
T

tom_usenet

Hello all,

Please see the question embedded in the code below. Thanks in advance for
your help!

Dave



#include <iostream>

using namespace std;

void foo(const char p[5])

That 5 above is ignored, the fact that you've written it makes it
unclear whether or not you realise this. The function takes any char
pointer.
{
// This yields char const * due to decay.
cout << "Non-template: " << typeid(p).name() << endl;
}

template <typename T>
void foo(T *p)
{
// This yields char; WHY NOT char const ????
cout << "Template: " << typeid(T).name() << endl;

// This yields char const *
cout << "Template: " << typeid(T *).name() << endl;
}

int main()
{
// This yields char const [5]. In sync with page 19 of the Standard;

Page numbers change, so it's better to quote paragraph numbers.
cool...
cout << typeid("test").name() << endl;

foo("test");
foo<>("test");
}

See 5.2.6/5 - typeid ignores top level cv-qualifiers. e.g.
typeid(int) == typeid(int const)

Tom
 
T

tom_usenet

Dave wrote in in comp.lang.c++:
Please see the question embedded in the code below. Thanks in advance
for your help!


[with T = char const]
// This yields char; WHY NOT char const ????
cout << "Template: " << typeid(T).name() << endl;

Because the type of "char const" is "char", const is just a qualifier.

IOW: char and char const are not distinct types.

But they are. An "object type" includes the cv-qualifiers. 3.9.3/1 is
very clear that the cv unqualified and cv qualified versions of a type
are "distinct types".

However, typeid ignores top level cv-qualifiers.

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top