By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 839 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Improper argument deduction???

P: n/a

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");
}
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Dave wrote in news:10*************@news.supernews.com 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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2

P: n/a
On Tue, 10 Aug 2004 17:13:27 -0700, "Dave" <be***********@yahoo.com>
wrote:

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
Jul 22 '05 #3

P: n/a
On 11 Aug 2004 00:36:07 GMT, Rob Williscroft <rt*@freenet.co.uk>
wrote:
Dave wrote in news:10*************@news.supernews.com 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
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.