This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: C++ toupper symbol clash?
- From: Xi Ruoyao <ryxi at stu dot xidian dot edu dot cn>
- To: Jonny Grant <jg at jguk dot org>, gcc-help at gcc dot gnu dot org
- Cc: ryxi at stu dot xidian dot edu dot cn
- Date: Sat, 05 Aug 2017 14:41:04 +0800
- Subject: Re: C++ toupper symbol clash?
- Authentication-results: sourceware.org; auth=none
- References: <d7abba0d-50cf-df68-d9b0-8bc0f12a6051@jguk.org>
On 2017-08-05 03:02 +0100, Jonny Grant wrote:
> Hello
>
> I thought functions can have the same name, because params are different
> in C++
Yes they can.
> eg my
> void tolower(std::string & str)
Yes it's correct.
> But I get a build error. I had expected not to conflict
>
> I'm not on this list, so please cc me in any replies.
> Thank you, Jonny
>
> int toupper( int ch );
> http://en.cppreference.com/w/cpp/string/byte/toupper
>
>
> $ g++ -O2 -Wall -Wextra -Wpedantic -o main main.cpp
> main.cpp: In function ‘void tolower(std::__cxx11::string&)’:
> main.cpp:23:66: error: no matching function for call to
> ‘transform(std::__cxx11::basic_string<char>::iterator,
> std::__cxx11::basic_string<char>::iterator,
> std::__cxx11::basic_string<char>::iterator, <unresolved overloaded
> function type>)’
> std::transform(str.begin(), str.end(), str.begin(), ::tolower);
> ^
> In file included from /usr/include/c++/5/algorithm:62:0,
> from main.cpp:3:
> /usr/include/c++/5/bits/stl_algo.h:4164:5: note: candidate:
> template<class _IIter, class _OIter, class _UnaryOperation> _OIter
> std::transform(_IIter, _IIter, _OIter, _UnaryOperation)
> transform(_InputIterator __first, _InputIterator __last,
> ^
> /usr/include/c++/5/bits/stl_algo.h:4164:5: note: template argument
> deduction/substitution failed:
> main.cpp:23:66: note: couldn't deduce template parameter ‘_UnaryOperation’
> std::transform(str.begin(), str.end(), str.begin(), ::tolower);
> ^
> In file included from /usr/include/c++/5/algorithm:62:0,
> from main.cpp:3:
> /usr/include/c++/5/bits/stl_algo.h:4201:5: note: candidate:
> template<class _IIter1, class _IIter2, class _OIter, class
> _BinaryOperation> _OIter std::transform(_IIter1, _IIter1, _IIter2,
> _OIter, _BinaryOperation)
> transform(_InputIterator1 __first1, _InputIterator1 __last1,
> ^
> /usr/include/c++/5/bits/stl_algo.h:4201:5: note: template argument
> deduction/substitution failed:
> main.cpp:23:66: note: candidate expects 5 arguments, 4 provided
> std::transform(str.begin(), str.end(), str.begin(), ::tolower);
>
>
>
>
>
> //g++ -O2 -Wall -Wextra -Wpedantic -o main main.cpp
>
> #include <algorithm>
> #include <string>
> #include <iostream>
>
> void myfunc(int i)
> {
> i = 10;
>
> std::cout << i;
> }
>
> void myfunc(std::string i)
> {
> i = "10";
>
> std::cout << i;
> }
>
> void tolower(std::string & str)
> {
> std::transform(str.begin(), str.end(), str.begin(), ::tolower);
> }
>
> int main (void)
> {
> std::string caps = "FOO";
>
> tolower(caps);
>
> return 0;
> }
This won't work. Reduced version:
int foo(int i);
char foo(char i);
template <typename T, typename Func>
void bar(T t, Func func);
int main (void)
{
bar('c', foo); // BOOM!
return 0;
}
It's impossible to deduce which type ‘Func’ should be at the
line with "BOOM!". Should it be char(*)(char) or int(*)(int)?
Note that we don't have the definition of 'bar', and don't know
how 'bar' would use 'func' so both are possible.
--
Xi Ruoyao <ryxi@stu.xidian.edu.cn>
School of Aerospace Science and Technology, Xidian University