[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

The top-level cv-qualifiers on the template-parameter are ignored when determining its type

greek_bill

11/26/2008 9:20:00 PM

I had some code that was failing to compile which amounts to something
like what's shown below. After some digging around, I came across
paragraph 14.1 note 5, which states :

"The top-level cv-qualifiers on the template-parameter are ignored
when determining its type."

Can someone comment on the reasoning behind this? It's probably
covered in something like 'The Design & Evolution of C++' but I don't
have a copy.

Thanks,

Bill

#include <iostream>
#include <typeinfo>

class Bar {};

template<class T>
void Func(T t)
{
std::cout << typeid(T).name() << "\n";
}

template<class T>
void Func(const T& t)
{
std::cout << "const ref : " << typeid(T).name() << "\n";
}


int main()
{
Bar bar;
const Bar& constBar = bar;

Func(constBar);

return 0;
}

gcc gives :

temp.cpp: In function 'int main()':
temp.cpp:24: error: call of overloaded 'Func(const Bar&)' is ambiguous
temp.cpp:7: note: candidates are: void Func(T) [with T = Bar]
temp.cpp:13: note: void Func(const T&) [with T = Bar]
3 Answers

Pete Becker

11/26/2008 9:52:00 PM

0

On 2008-11-26 16:20:24 -0500, greek_bill <greek_bill@yahoo.com> said:

> I had some code that was failing to compile which amounts to something
> like what's shown below. After some digging around, I came across
> paragraph 14.1 note 5, which states :
>
> "The top-level cv-qualifiers on the template-parameter are ignored
> when determining its type."
>
> Can someone comment on the reasoning behind this?

This has nothiing to do with the problem.
>
> #include <iostream>
> #include <typeinfo>
>
> class Bar {};
>
> template<class T>
> void Func(T t)
> {
> std::cout << typeid(T).name() << "\n";
> }
>
> template<class T>
> void Func(const T& t)
> {
> std::cout << "const ref : " << typeid(T).name() << "\n";
> }
>
>
> int main()
> {
> Bar bar;
> const Bar& constBar = bar;
>
> Func(constBar);
>
> return 0;
> }
>
> gcc gives :
>
> temp.cpp: In function 'int main()':
> temp.cpp:24: error: call of overloaded 'Func(const Bar&)' is ambiguous
> temp.cpp:7: note: candidates are: void Func(T) [with T = Bar]
> temp.cpp:13: note: void Func(const T&) [with T = Bar]

Right: the call is ambiguous. When you're puzzled by error messages
that involve templates, try something similar without templates.

void f(Bar);
void f(const Bar&);
Bar b;
f(b); // same problem: ambiguous. No preference between Bar and const Bar&.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

James Kanze

11/27/2008 8:45:00 AM

0

On Nov 26, 10:20 pm, greek_bill <greek_b...@yahoo.com> wrote:
> I had some code that was failing to compile which amounts to
> something like what's shown below. After some digging around,
> I came across paragraph 14.1 note 5, which states :

> "The top-level cv-qualifiers on the template-parameter are
> ignored when determining its type."

> Can someone comment on the reasoning behind this?

Because they are ignored for non-template functions? The
signatures:
void f( int ) ;
and
void f( int const ) ;
are identical.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

greek_bill

11/27/2008 8:02:00 PM

0

Thank you both for the replies.

Can I then get you to look at the following bit of code, which is what
prompted my original question?

Basically I want to declare as a friend an instance of a template for
a reference to a const (which I think I'm doing correctly below). The
trouble I'm having is instantiating the template without explicitly
providing the template arguments.

thanks again!


template<class T>
void Func(T t)
{
t.PrivateFunc();
}

class Bar
{
private:
void PrivateFunc() const
{}

friend void Func<const Bar&>(const Bar&);
};

int main()
{
Bar bar;
const Bar& constBar = bar;

// This doesn't work...
Func(constBar);

// ...but this does!
Func<const Bar&>(constBar);

return 0;
}