71

I tried the code from this question C++ std::transform() and toupper() ..why does this fail?

#include <iostream>
#include <algorithm>

int main() {
  std::string s="hello";
  std::string out;
  std::transform(s.begin(), s.end(), std::back_inserter(out), std::toupper);
  std::cout << "hello in upper case: " << out << std::endl;
}

Theoretically it should've worked as it's one of the examples in Josuttis' book, but it doesn't compile http://ideone.com/aYnfv.

Why did GCC complain:

no matching function for call to ‘transform(
    __gnu_cxx::__normal_iterator<char*, std::basic_string
        <char, std::char_traits<char>, std::allocator<char> > >, 
    __gnu_cxx::__normal_iterator<char*, std::basic_string
        <char, std::char_traits<char>, std::allocator<char> > >, 
    std::back_insert_iterator<std::basic_string
        <char, std::char_traits<char>, std::allocator<char> > >,
    <unresolved overloaded function type>)’

Am I missing something here? Is it GCC related problem?

3
  • 1
    The one in the std namespace does not match the function prototype. So it complains.
    – user195488
    Aug 20, 2011 at 13:00
  • 2
    @CodeMonkey: There is more than one. So it complains. Aug 20, 2011 at 13:39
  • @Tomalak: Yes, same difference.
    – user195488
    Aug 20, 2011 at 15:16

3 Answers 3

85

Just use ::toupper instead of std::toupper. That is, toupper defined in the global namespace, instead of the one defined in std namespace.

std::transform(s.begin(), s.end(), std::back_inserter(out), ::toupper);

Its working : http://ideone.com/XURh7

Reason why your code is not working : there is another overloaded function toupper in the namespace std which is causing problem when resolving the name, because compiler is unable to decide which overload you're referring to, when you simply pass std::toupper. That is why the compiler is saying unresolved overloaded function type in the error message, which indicates the presence of overload(s).

So to help the compiler in resolving to the correct overload, you've to cast std::toupper as

(int (*)(int))std::toupper

That is, the following would work:

//see the last argument, how it is casted to appropriate type
std::transform(s.begin(), s.end(), std::back_inserter(out),(int (*)(int))std::toupper);

Check it out yourself: http://ideone.com/8A6iV

15
  • This indeed works, but why? toupper is also in the std namespace (via <cctype>). Aug 20, 2011 at 13:01
  • 2
    @Tomalak: That is what I said "..overloaded function toupper in the namespace std" which means std::toupper has overload.
    – Nawaz
    Aug 20, 2011 at 13:16
  • 2
    @Oli: <locale> defines template <class charT> charT toupper(charT c, const locale& loc) Aug 20, 2011 at 13:27
  • 5
    If you do use ::toupper, it would be best to include the C header (<ctype.h>) to make sure it's in the global namespace. Then you'll be relying on deprecated but well-specified behaviour, not unspecified behaviour. Aug 20, 2011 at 13:29
  • 2
    Sorry, but the opening to this answer ("Just use toupper instead of std::toupper") is incomplete. If the OP uses using namespace std then it ceases to be a viable workaround (not to mention that you're either using deprecated or unspecified behaviour even when it does work). Aug 20, 2011 at 15:11
51

Problem

std::transform(
    s.begin(),
    s.end(),
    std::back_inserter(out),
    std::toupper
);

no matching function for call to ‘transform(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::back_insert_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, <unresolved overloaded function type>)

This is a misleading error; the interesting part is not that there's "no matching function" for the call, but why there's no matching function.

The why is that you're passing a function reference of an "<unresolved overloaded function type>" as an argument, and GCC prefers to error on the call rather than on this overload resolution failure.


Explanation

First, you should consider how the C library is inherited in C++. <ctype.h> has a function int toupper(int).

C++ inherits this:

[n3290: 21.7/1]: Tables 74, 75, 76, 77, 78, and 79 describe headers <cctype>, <cwctype>, <cstring>, <cwchar>, <cstdlib> (character conversions), and <cuchar>, respectively.

[n3290: 21.7/2]: The contents of these headers shall be the same as the Standard C Library headers <ctype.h>, <wctype.h>, <string.h>, <wchar.h>, and <stdlib.h> and the C Unicode TR header <uchar.h>, respectively [..]

[n3290: 17.6.1.2/6]:Names that are defined as functions in C shall be defined as functions in the C++ standard library.

But using <ctype.h> is deprecated:

[n3290: C.3.1/1]: For compatibility with the Standard C library, the C++ standard library provides the 18 C headers (D.5), but their use is deprecated in C++.

And the way to access the C toupper is through the C++ backwards-compatibility header <cctype>. For such headers, the contents are either moved or copied (depending on your implementation) into the std namespace:

[n3290: 17.6.1.2/4]: [..] In the C++ standard library, however, the declarations (except for names which are defined as macros in C) are within namespace scope (3.3.6) of the namespace std. It is unspecified whether these names are first declared within the global namespace scope and are then injected into namespace std by explicit using-declarations (7.3.3).

But the C++ library also introduces a new, locale-specific function template in header <locale>, that's also called toupper (of course, in namespace std):

[n3290: 22.2]: [..] template <class charT> charT toupper(charT c, const locale& loc); [..]

So, when you use std::toupper, there are two overloads to choose from. Since you didn't tell GCC which function you wish to use, the overload cannot be resolved and your call to std::transform cannot be completed.


Disparity

Now, the OP of that original question did not run into this problem. He likely did not have the locale version of std::toupper in scope, but then again you didn't #include <locale> either!

However:

[n3290: 17.6.5.2]: A C++ header may include other C++ headers.

So it just so happens that either your <iostream> or your <algorithm>, or headers that those headers include, or headers that those headers include (etc), lead to the inclusion of <locale> on your implementation.


Solution

There are two workarounds to this.

  1. You may provide a conversion clause to coerce the function pointer into referring to the overload that you wish to use:

    std::transform(
       s.begin(),
       s.end(),
       std::back_inserter(out),
       (int (*)(int))std::toupper  // specific overload requested
    );
    
  2. You may remove the locale version from the overload set by explicitly using the global toupper:

    std::transform(
       s.begin(),
       s.end(),
       std::back_inserter(out),
       ::toupper                  // global scope
    );
    

    However, recall that whether or not this function in <cctype> is available is unspecified ([17.6.1.2/4]), and using <ctype.h> is deprecated ([C.3.1/1]).

    Thus, this is not the option that I would recommend.


(Note: I despise writing angle brackets as if they were part of header names — they are part of #include syntax, not header names — but I've done it here for consistency with the FDIS quotes; and, to be honest, it is clearer...)

11
  • 7
    Note: This is all slightly silly, because std::transform is, itself, looking for a specific function signature so in theory the compiler could deduce solution 1 for you. But, well, it's complicated; and welcome to C++. Aug 20, 2011 at 13:40
  • 1
    a bit late to the party, but I still think the compiler should AT LEAST list the ambigous options.
    – Vargas
    Aug 14, 2015 at 13:06
  • 1
    @Vargas: It can't! The options don't exist (or there are infinite options, depending on how you look at it) until std::transform (a template) has been instantiated, which cannot happen until the type of the final argument (the functor) is known. Chicken vs egg :) Aug 14, 2015 at 13:19
  • 1
    @JustinTime: Don't forget that standard headers are allowed to include other standard headers and it's unspecified which (if any) those will be, under any given implementation. Mar 7, 2016 at 22:27
  • 2
    ...And it seems that's the issue, or at least a significant part of it. To determine if this was the reason for the difference in behaviour, I wrote a simple program that reads a string, transforms it with toupper(), & outputs the result. It includes <iostream>, <string>, <cctype>, <locale>, & <algorithm>. It seems that, interestingly enough, MSVC 2010 is able to correctly deduce which version of toupper() you want, while later versions (or at least 2015) can't. MSVC 2015 is fine as long as you don't explicitly include <locale>, but it can't resolve the overload if you do. Mar 7, 2016 at 23:37
0
std::transform(s.begin(), s.end(), s.begin(), 
std::bind(&std::toupper<char>, std::placeholders::_1, std::locale()));

If you use vc toolchain,please include locale

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.