tags:

views:

135

answers:

3

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.

My code looks like this:

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

It gives this compilation error:

In function 'int main()'  
error: call of overloaded 'Func(const Bar&)' is ambiguous

Can someone comment on the reasoning behind the this rule in the standard?

+4  A: 

The problem with your code is that the function call is ambiguous. The const Bar & can match either the value or the const reference. G++ says:

xx.cpp:24: error: call of overloaded 'Func(const Bar&)' is ambiguous

This has nothing specifically to do with templates - you would get the same error if you overloaded a non-template function.

And as people have told you here time after time, you will not learn C++ by reading the Standard.

anon
@Neil:Iam not studying or reading from Standards..Iam doing my project.Any doubts see my profile Why iam here..Thanks for ur valuable suggestion :)
BE Student
@Neil:Check that qualifiers are allowed in nontype parameters.. Note that "top-level" qualifiers have no effect.
BE Student
@BE Student If you are not reading the standard then the fact that para 14.1/5 of that document contains the exact text you quoted must be a fantastic coincidence! and your second comment makes no sense.
anon
Iam a student of B-tech in Computer Science EngineeringI am doing the Project on c++ Compiler Validation..We selected some compilers and we need to submit the report of ranking the compilers...according to the c++ ISO standard Especially on Templates.
BE Student
And my second comment is...iam not simply asking doubts from standards...I posted..that where i exactly found a problem..I thouhgt u may help....help
BE Student
@BE Student From your posts here, you are about as qualified to evaluate C++ compilers as I would be to swim the Atlantic. And as I pointed out, the error you are getting has nothing particularly to do with templates.
anon
OH .Thanks for your Posts
BE Student
+1  A: 

The call is ambiguous because anything can match T or const T &.

Just try Func(0);: it will give the same error message.

Didier Trosset
Check that qualifiers are allowed in nontype parameters.. Note that "top-level" qualifiers have no effect.
BE Student
+1  A: 

As you could have found easily yourself, this has nothing to do with templates. This

class Bar {};

void Func(Bar) {}
void Func(const Bar&) {}

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

    Func(bar);
    Func(constBar);

    return 0;
}

gives the same errors.

sbi
How can you solve that ambiguity? I may want to use Func(bar) to pass bar by value, expecting a copy to be created, and Func(constBar) to pass constBar by reference.
rturrado
@rturrado: You can't. Both will always be an equally good match.
sbi