0

I was trying to make this small example work

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<char> v{'a', 'b', 'c', 'd'};
    std::transform(v.begin(), v.end(), v.begin(), std::toupper);
    std::for_each(v.begin(), v.end(), [](char & c) {
                    std::cout << c << " ";
                  });
}

I think it's obvious, what i'm trying to achive. However, I get these errors

    ||=== Build file: "no target" in "no project" (compiler: unknown) ===|
C:\Users\vitaly.bushaev\Documents\vk cup\1.cpp||In function 'int main()':|
C:\Users\vitaly.bushaev\Documents\vk cup\1.cpp|8|error: no matching function for call to 'transform(std::vector<char>::iterator, std::vector<char>::iterator, std::vector<char>::iterator, <unresolved overloaded function type>)'|
C:\Users\vitaly.bushaev\Documents\vk cup\1.cpp|8|note: candidates are:|
C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_algo.h|4152|note: template<class _IIter, class _OIter, class _UnaryOperation> _OIter std::transform(_IIter, _IIter, _OIter, _UnaryOperation)|
C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_algo.h|4152|note:   template argument deduction/substitution failed:|
C:\Users\vitaly.bushaev\Documents\vk cup\1.cpp|8|note:   couldn't deduce template parameter '_UnaryOperation'|
C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_algo.h|4189|note: template<class _IIter1, class _IIter2, class _OIter, class _BinaryOperation> _OIter std::transform(_IIter1, _IIter1, _IIter2, _OIter, _BinaryOperation)|
C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_algo.h|4189|note:   template argument deduction/substitution failed:|
C:\Users\vitaly.bushaev\Documents\vk cup\1.cpp|8|note:   candidate expects 5 arguments, 4 provided|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

So I'm reading this and I understand the error, but i can't understand why is it happening. Sure this works fine:

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<char> v{'a', 'b', 'c', 'd'};
    using it = std::vector<char>::iterator;
    std::transform<it, it, int (int)>(v.begin(), v.end(), v.begin(), std::toupper);
    std::for_each(v.begin(), v.end(), [](char & c) {
                    std::cout << c << " ";
                  });
}

So here's the question:

1) Why c++ can't deduce template parameter here ?

2) Is there a way around that, to not specify types ?

1 Answers1

3

There is more than one overload for std::toupper.

Template deduction happens before overload selection, so you have to either be specific or wrap the call in a lambda or function object.

#include <iostream>
#include <vector>
#include <algorithm>



int main() {
    std::vector<char> v{'a', 'b', 'c', 'd'};

    // method 1
    struct make_upper
    {
        int operator()(int ch) const { return std::toupper(ch); }
    };
    std::transform(v.begin(), v.end(), v.begin(), make_upper());

    // method 2
    std::transform(v.begin(), v.end(), v.begin(), [](auto&& ch) { return std::toupper(ch); });
}

references:

http://en.cppreference.com/w/cpp/locale/toupper http://en.cppreference.com/w/cpp/string/byte/toupper

Richard Hodges
  • 68,278
  • 7
  • 90
  • 142