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

G

greek_bill

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]
 
J

James Kanze

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.
 
G

greek_bill

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;
}
 

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,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top