Discussion:
Ambiguous call
(too old to reply)
Vladimir Grigoriev
2009-11-17 14:06:27 UTC
Permalink
There is a function template in the std namespace. And a function with the
same name is defined in a program. Is it correct that the compiler issues an
error about ambiguous call then the function is called without std:: prefix?

For example

#include <functional>

template <typename InputIterator, typename T>
inline const T accumulate( Input Iterator first, InputIterator last, const T
&val )
{
T result = val;

for ( ; first != last; ++first )
{
result = result + *first;
}

return ( result );
}

int _tmain(int argc, _TCHAR* argv[])

{
std::vector<int> vi;
...
int sum = accumulate( vi.begin(), vi.end(), 0 ); // <== here is the
error
...
return 0;
}

Vladimir Grigoriev
Victor Bazarov
2009-11-17 14:22:20 UTC
Permalink
Post by Vladimir Grigoriev
There is a function template in the std namespace. And a function with the
same name is defined in a program. Is it correct that the compiler issues an
error about ambiguous call then the function is called without std:: prefix?
For example
#include <functional>
template <typename InputIterator, typename T>
inline const T accumulate( Input Iterator first, InputIterator last, const T
&val )
{
T result = val;
for ( ; first != last; ++first )
{
result = result + *first;
}
return ( result );
}
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<int> vi;
...
int sum = accumulate( vi.begin(), vi.end(), 0 ); // <== here is the
error
...
return 0;
}
Vladimir Grigoriev
See ADL (nickname "Koenig lookup") - namespace 'std' is added to the
lookup areas because of the arguments (which are of types declared in
'std'). At least that's how I understand it. If you want to limit the
lookup to global namespace only, qualify your name:

int sum = ::accumulate( ...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Vladimir Grigoriev
2009-11-17 14:46:45 UTC
Permalink
Thanks, Victor.

I should make a remark that I included an incorrect header file. Should be
<numeric> instead of <functional>.:) . However another question arises: How
does the compiler know that there is such name as std::accumulate if I did
not include the header <numeric>?

Vladimir Grigoriev
Post by Victor Bazarov
See ADL (nickname "Koenig lookup") - namespace 'std' is added to the
lookup areas because of the arguments (which are of types declared in
'std'). At least that's how I understand it. If you want to limit the
int sum = ::accumulate( ...
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Vladimir Grigoriev
2009-11-17 15:10:18 UTC
Permalink
I am sorry. In my real example there was the <numeric> header! So the
question is denied.

Vladimir Grigoriev
Post by Vladimir Grigoriev
Thanks, Victor.
I should make a remark that I included an incorrect header file. Should be
How does the compiler know that there is such name as std::accumulate if I
did not include the header <numeric>?
Vladimir Grigoriev
Post by Victor Bazarov
See ADL (nickname "Koenig lookup") - namespace 'std' is added to the
lookup areas because of the arguments (which are of types declared in
'std'). At least that's how I understand it. If you want to limit the
int sum = ::accumulate( ...
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Igor Tandetnik
2009-11-17 14:25:35 UTC
Permalink
Post by Vladimir Grigoriev
There is a function template in the std namespace. And a function with the
same name is defined in a program. Is it correct that the compiler issues an
error about ambiguous call then the function is called without std:: prefix?
Yes. In your case, std::accumulate is found by argument-dependent loopkup (ADL, aka Koenig lookup), and ::accumulate by a normal lookup. This renders the use of unqualified name ambiguous.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Continue reading on narkive:
Loading...