All I'm trying to do is implement the equal algorithm. However, when I test with a few strings, I get an ambiguity error. I think the compiler cannot differentiate between the A and the B. Why is this?
template <class A, class B> bool equal(A beg, A end, B out)
{
while(beg != end) {
if(*beg == *out) {
++beg;
++out;
}
else return false;
}
return true;
}
MAIN
std::string a("This is a string");
std::string b("This is a string");
std::string c("String c");
std::cout << "a and b are " << equal(a.begin(), a.end(), b.begin()) << std::endl;
std::cout << "a and c are " << equal(a.begin(), a.end(), c.begin()) << std::endl;
ERROR MESSAGE
procedures_main.cpp:17:35: error: call to 'equal' is ambiguous
std::cout << "a and b is " << equal(a.begin(), a.end(), b.begin()) << std::endl;
^~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/algorithm:1105:1: note:
candidate function [with _InputIterator1 = std::__1::__wrap_iter<char *>, _InputIterator2 =
std::__1::__wrap_iter<char *>]
equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
^
./procedures.hpp:73:34: note: candidate function [with A = std::__1::__wrap_iter<char *>, B = std::__1::__wrap_iter<char
*>]
template <class A, class B> bool equal(A beg, A end, B out)
The problem is that the arguments (the iterators from std::string) are in the namespace std and in this namespace, there is another algorithm called equal which is a candidate due to argument dependent lookup (ADL). You need to explicitly qualify your algorithm:
std::cout << "a and b are " << ::equal(a.begin(), a.end(), b.begin()) << std::endl;
// ^^ here
Note that the C++ standard does not require the iterators to be a type in std, but is allows it and you compiler/standard library decided to use this option.
This is the result of the so-called Argument-Dependent Name Lookup. There is standard algorithm std::equal in C++. The compiler sees that arguments of your function call belong to namespace std. So it also takes into consideration any function with name equal in namespace std. As the result it finds two functions: the one that is defined by you and the other that is declared in namespace std.
To escape the error use fully qualified name of your function that is ::equal.
By the way you use your function incorrectly and such a usage has undefined behaviour. The second range must have at least the same size as the first range. In your example wnen you use strings a and c the size of c is less than size of a.
Related
I've run into some difficulty with the view::join function object of the (amazing) Ranges-v3 library. My client code is dependent upon the presence of a back method (and would greatly appreciate random access iterators) for an aggregate view of a collection of ranges.
After reviewing the relevant documentation it seems that a back method is compatible with instantations of the join_view class template, but I've been unable to instantiate it as such.
#include <iostream>
#include <vector>
#include <range/v3/all.hpp>
struct Foo{
std::vector<int> i = {1,2,3,4};
const std::vector<int>& data() const { return this->i; }
};
int main(){
std::vector< Foo > foos = { Foo(), Foo(), Foo() };
auto data = []( auto&& foo ){ return foo.data() | ranges::view::all; };
auto flat = foos | ranges::view::transform(data) | ranges::view::join;
std::cout << flat.back() << std::endl; // compiler error
}
The relevant bits of the compiler error message are:
main.cpp:17:28: error: no matching function for call to 'ranges::v3::join_view<ranges::v3::transform_view<ranges::v3::range<__gnu_cxx::__normal_iterator<Foo*, std::vector<Foo> >, __gnu_cxx::__normal_iterator<Foo*, std::vector<Foo> > >, main()::<lambda(auto:1&&)> >, void>::back()'
std::cout << flat.back() << std::endl; // compiler error
/usr/local/include/range/v3/range_interface.hpp:116:34: note: candidate: template<class D, int _concept_requires_115, typename std::enable_if<((_concept_requires_115 == 43) || ((std::is_same<D, ranges::v3::join_view<ranges::v3::transform_view<ranges::v3::range<__gnu_cxx::__normal_iterator<Foo*, std::vector<Foo> >, __gnu_cxx::__normal_iterator<Foo*, std::vector<Foo> > >, main()::<lambda(auto:1&&)> >, void> >() && ranges::v3::concepts::models<ranges::v3::concepts::BoundedView, T>()) && ranges::v3::concepts::models<ranges::v3::concepts::BidirectionalView, T>())), int>::type <anonymous> > ranges::v3::range_reference_t<D> ranges::v3::range_interface<Derived, Inf>::back() [with D = D; int _concept_requires_115 = _concept_requires_115; typename std::enable_if<((_concept_requires_115 == 43) || ((std::is_same<D, Derived>() && ranges::v3::concepts::models<ranges::v3::concepts::BoundedView, D>()) && ranges::v3::concepts::models<ranges::v3::concepts::BidirectionalView, D>())), int>::type <anonymous> = <enumerator>; Derived = ranges::v3::join_view<ranges::v3::transform_view<ranges::v3::range<__gnu_cxx::__normal_iterator<Foo*, std::vector<Foo> >, __gnu_cxx::__normal_iterator<Foo*, std::vector<Foo> > >, main()::<lambda(auto:1&&)> >, void>; bool Inf = false]
range_reference_t<D> back()
/usr/local/include/range/v3/range_interface.hpp:115:17: error: no type named 'type' in 'struct std::enable_if<false, int>'
CONCEPT_REQUIRES_(Same<D, Derived>() && BoundedView<D>() && BidirectionalView<D>())>
The first requirement seems to be enforcing proper use of the CRTP, which is satisfied. So the join_view violates either the BoundedView or BidirectionalView concepts (or both). I was able to quickly eliminate the former as a possiblity.
auto flat = foos
| ranges::view::transform(data)
| ranges::view::join
| ranges::view::bounded;
std::cout << flat.back() << std::endl; // compiler error
In this case, flat satisfies the BoundedView concept, but the error message remains unchanged.
To verify the BidirectionalView, I attempted examined the iterators of the join_view, but encountered (what is suspect) is a bug.
auto it = flat.begin();
std::cout << *it << std::endl; // correct
++it; std::cout << *it << std::endl; // correct
--it; std::cout << *it << std::endl; // doesn't actually decrement
auto other = --it;
std::cout << *it << ' ' << *other << std::endl; // also doesn't decrement
I've written up a live version for easy inspection.
Has anyone had any luck instantiating a bidirectional join_view? Any suggestion of how I might achieve similar behavior without copying the underlying data?
Range-v3's join view satisfies InputRange, but not Forward or anything stronger. It has to do with how the join is accomplished. While iterating over an inner range, the range needs to be stored somewhere. That somewhere is in a member of the join_view object. In other words, join_view is mutated while you iterate over it. For that reason, it cannot model any range category stronger than Input.
#include <iostream>
using namespace std;
template <typename T>
T max(T x, T y)
{
return (x > y) ? x : y;
}
int main()
{
cout << max(3, 7) << std::endl;
cout << max(3.0, 7.0) << std::endl;
cout << max(3, 7.0) << std::endl;
return 0;
}
I'm Expecting max's Instance here
cout << max(3, 7) << std::endl; // max (int, int)
cout << max(3.0, 7.0) << std::endl; // max (double, double)
cout << max(3, 7.0) << std::endl; // max(int, double)
Then what is the problem ? Why I'm getting
11 25 [Error] call of overloaded 'max(double, double)' is ambiguous
If you view the compile error in full, you'd see why. Here's what gcc 5.2 gives me:
main.cpp: In function 'int main()':
main.cpp:10:21: error: call of overloaded 'max(int, int)' is ambiguous
cout << max(3, 7) << std::endl;
^
main.cpp:4:3: note: candidate: T max(T, T) [with T = int]
T max(T x, T y)
^
In file included from /usr/local/include/c++/5.2.0/bits/char_traits.h:39:0,
from /usr/local/include/c++/5.2.0/ios:40,
from /usr/local/include/c++/5.2.0/ostream:38,
from /usr/local/include/c++/5.2.0/iostream:39,
from main.cpp:1:
/usr/local/include/c++/5.2.0/bits/stl_algobase.h:219:5: note: candidate: constexpr const _Tp& std::max(const _Tp&, const _Tp&) [with _Tp = int]
max(const _Tp& __a, const _Tp& __b)
^
Basically, there are two max functions - yours and std::max, which is included by some chain of other #includes from <iostream>. The latter is found by lookup because of your
using namespace std;
Effectively, we have:
template <typename T> T max(T, T); // yours
template <typename T> T const& max(T const&, T const&); // std
Neither is better than the other, hence ambiguous. This is a great reason to avoid using namespace std. Or a great reason to not reinvent the wheel when it comes to standard library functions - just use std::max. Or both.
On the other hand, this one
max(3, 7.0)
will fail regardless as a template deduction failure. It would deduce T as int for the first argument, and T as double for the second - but there can only be one T! You would have to explicitly call either max<int>(3, 7.0) or max<double>(3, 7.0) to get around the deduction failure, depending on which of the two arguments you want to cast.
The line
using namespace std;
makes things complicated, for sure. However, even after you remove that line, the problem continues to exist.
The call max(3, 7.0) can be resolved to max<int> or max<double>. To resolve to max<int> a double has to be converted to an int. To resolve to max<double>, an int has to be converted to a double. Since both require a conversion and one conversion cannot be given higher priority than the other, the compiler is not able to resolve which one to use.
You'll have to be explicit which version of max you want to use.
max<int>(3, 7.0) will convert 7.0, a double, to an int.
max<double>(3, 7.0) will convert 3, an int, to a double.
Max is a library function.just change the max identifier for your function to max1 or any other name that is not a function defined any header files.
That should solve your problem.
I have the following code.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
template <typename Type> inline Type max(Type t1, Type t2) {
return t1 > t2 ? t1 : t2;
}
template <typename Type> inline Type max(const std::vector<Type> &vec) {
return *std::max_element(vec.begin(),vec.end());
}
template <typename Type> inline Type max(const Type *parray, int size) {
return *std::max_element(parray,parray+size);
}
int main(int argc, char *argv[]) {
std::string sarray[] = {"we","were","her","pride","of","ten"};
std::vector<std::string> svec(sarray,sarray+6);
int iarray[] = {12,70,2,169,1,5,29};
std::vector<int> ivec(iarray,iarray+7);
float farray[] = {2.5,24.8,18.7,4.1,23.9};
std::vector<float> fvec(farray,farray+5);
int imax = max(max(ivec),max(iarray,7));
float fmax = max(max(fvec),max(farray,5));
std::string smax = max(max(svec),max(sarray,6));
std::cout << "imax should be 169 -- found: " << imax << '\n'
<< "fmax should be 24.8 -- found: " << fmax << '\n'
<< "smax should be were -- found: " << smax << '\n';
return 0;
}
I am attempting to implement two simple template functions to output the max element of a vector and an array. However, I am receiving the following error when the type is a string.
error: call of overloaded 'max(std::string, std::string)' is ambiguous
Why is this occuring, and what is the best way to remedy it?
The problem is that the compiler is finding multiple matching definitions of max via ADL and it doesn't know which to choose.
Try changing the call to max to use its qualified-id:
std::string smax = ::max(max(svec),max(sarray,6));
Your code
std::string smax = max(max(svec),max(sarray,6));
translates to:
std::string smax = max(string ,string );
After max(svec) and max(sarray,6) are evaluated using your templates. Now here the problem arises:
The standard library already comes with a templated max() function. The compiler will be unable to tell whether you want your version of max() or std::max().
Now you will ask why it worked for integers and floats. Answer is in this line you are specifically mentioning std::string. Hence the compiler is getting confused.
There can be work arounds. But since you need the best solution, I would say rename your max function say MAximum.
Why is this occuring?
The compiler error already tells you why. It does not know which version of max to use.
note: candidates are:
main.cpp:6:38: note: Type max(Type, Type) [with Type = std::basic_string]
...
/usr/include/c++/4.7/bits/stl_algobase.h:210:5: note: const _Tp& std::max(const _Tp&, const _Tp&) [with _Tp = std::basic_string]
Solution:
either explicitely call your max function or just call std::max (which already exists, so why would you want to reimplement it?).
Additionally, there is one ; too much after << "fmax should be 24.8 -- found: " << fmax << '\n'
I'm trying to use an ANSI C++ for_each statement to iterate over and print the elements of a standard vector. It works if I have the for_each call a non-overloaded function, but yields a compiler error if I have it call an overloaded function.
Here's a minimal test program to show where the compiler error occurs:
#include <algorithm>
#include <iostream>
#include <vector>
struct S {
char c;
int i;
};
std::vector<S> v;
void print_struct(int idx);
void print_struct(const struct S& s);
// f: a non-overloaded version of the preceding function.
void f(const struct S& s);
int main()
{
v.push_back((struct S){'a', 1});
v.push_back((struct S){'b', 2});
v.push_back((struct S){'c', 3});
for (unsigned int i = 0; i < v.size(); ++i)
print_struct(i);
/* ERROR! */
std::for_each(v.begin(), v.end(), print_struct);
/* WORKAROUND: */
std::for_each(v.begin(), v.end(), f);
return 0;
}
// print_struct: Print a struct by its index in vector v.
void print_struct(int idx)
{
std::cout << v[idx].c << ',' << v[idx].i << '\n';
}
// print_struct: Print a struct by reference.
void print_struct(const struct S& s)
{
std::cout << s.c << ',' << s.i << '\n';
}
// f: a non-overloaded version of the preceding function.
void f(const struct S& s)
{
std::cout << s.c << ',' << s.i << '\n';
}
I compiled this in openSUSE 12.2 using:
g++-4.7 -ansi -Wall for_each.cpp -o for_each
The full error message is:
for_each.cpp: In function ‘int main()’:
for_each.cpp:31:48: error: no matching function for call to ‘for_each(std::vector<S>::iterator, std::vector<S>::iterator, <unresolved overloaded function type>)’
for_each.cpp:31:48: note: candidate is:
In file included from /usr/include/c++/4.7/algorithm:63:0,
from for_each.cpp:5:
/usr/include/c++/4.7/bits/stl_algo.h:4436:5: note: template<class _IIter, class _Funct> _Funct std::for_each(_IIter, _IIter, _Funct)
/usr/include/c++/4.7/bits/stl_algo.h:4436:5: note: template argument deduction/substitution failed:
for_each.cpp:31:48: note: couldn't deduce template parameter ‘_Funct’
I don't see any search results for this particular error on Stack Overflow, or on the web generally. Any help would be appreciated.
A names refers to an overload set. You'll need to specify which overload you want:
std::for_each(v.begin(), v.end(), (void (&)(S const&)) print_struct);
Another approach is to use a polymorphic callable function object as a helper:
struct PrintStruct
{
template <typename T> void operator()(T const& v) const
{ return print_struct(v); }
};
int main()
{
PrintStruct helper;
std::vector<S> sv;
std::vector<int> iv;
// helper works for both:
std::for_each(sv.begin(), sv.end(), helper);
std::for_each(iv.begin(), iv.end(), helper);
std::for_each declaration looks like this:
template<class InputIter, class Func>
void for_each(InputIter first, InputIter last, Func func);
As you can see, it takes anything you give it as the third parameter. There is no restriction that it has to be a callable type of a certain signature or a callable type at all.
When dealing with overloaded functions, they're inherently ambiguous unless you give them some context to select the right one. In a call to an overloaded function, this context are the arguments you pass. When you need a pointer, however, you can't use arguments as a context, and the for_each parameter also doesn't count as a context, since it takes anything.
As an example of where a function parameter can be a valid context to select the right overload, see this:
// our overloads
void f(int){}
void f(double){}
typedef void (*funcptr_type)(int);
void g(funcptr_type){}
// ...
g(&f); // will select 'void f(int)' overload, since that's
// the only valid one given 'g's parameter
As you can see, you give a clear context here that helps the compiler select the right overload and not have it ambiguous. std::for_each's parameters do not give such a context, since they take anything.
There are two solutions:
manually provide the context either by
casting to the right function pointer type, or
using an intermediate variable of the right type and passing that
use a non-overloaded function that dispatches to an overloaded one (as you did with f)
Note that in C++11, you could also use a lambda for the second option:
std::for_each(v.begin(), v.end(), [](const S& s){ print_struct(s); });
Some notes on your code:
(struct S){'a', 1} is a compound literal and not standard C++
you don't need struct S in C++, only S suffices
I'm trying to write a function to print a representation of common STL containers (vector, list, etc..). I gave the function a template parameter T which, for example, might represent vector. I'm having problems getting an iterator of type T.
vector<int> v(10, 0);
repr< vector<int> >(v);
...
template <typename T>
void repr(const T & v)
{
cout << "[";
if (!v.empty())
{
cout << ' ';
T::iterator i;
for (i = v.begin();
i != v.end()-1;
++i)
{
cout << *i << ", ";
}
cout << *(++i) << ' ';
}
cout << "]\n";
}
...
brett#brett-laptop:~/Desktop/stl$ g++ -Wall main.cpp
main.cpp: In function ‘void repr(const T&)’:
main.cpp:13: error: expected ‘;’ before ‘i’
main.cpp:14: error: ‘i’ was not declared in this scope
main.cpp: In function ‘void repr(const T&) [with T = std::vector<int, std::allocator<int> >]’:
main.cpp:33: instantiated from here
main.cpp:13: error: dependent-name ‘T::iterator’ is parsed as a non-type, but instantiation yields a type
main.cpp:13: note: say ‘typename T::iterator’ if a type is meant
I tried 'typename T::iterator' as the compiler suggested, but only got a more cryptic error.
Edit: Thanks for the help guys! Here's a working version for anyone who wants to use this function:
template <typename T>
void repr(const T & v)
{
cout << "[";
if (!v.empty())
{
cout << ' ';
typename T::const_iterator i;
for (i = v.begin();
i != v.end();
++i)
{
if (i != v.begin())
{
cout << ", ";
}
cout << *i;
}
cout << ' ';
}
cout << "]\n";
}
You need typename to tell the compiler that ::iterator is supposed to be a type. The compiler doesn't know that it's a type because it doesn't know what T is until you instantiate the template. It could also refer to some static data member, for example. That's your first error.
Your second error is that v is a reference-to-const. So, instead of ::iterator you have to use ::const_iterator. You can't ask a constant container for a non-const iterator.
Change T::iterator i; to typename T::const_iterator i; because ::iterator is of type T and v is a const &.
Before a qualified dependent type, you need typename.
Without typename, there is a C++ parsing rule that says that qualified dependent names should be parsed as non-types even if it leads to a syntax error.
typename states that the name that follows should be treated as a type. Otherwise, names are interpreted to refer to non-types.
Maybe this will help:
Typename is mandatory before a qualified-dependent name which refers to a type, unless that name is naming a base class, or in an initialization list. Typename is optional before a qualified (but non-dependent name) is used within a template, except again when naming a base class or in an initialization list.