6

我正在尝试使用 boost::to_lower_copy 和 std::transform 来小写一堆字符串。如下所示,变体 1,使用 lamdba 有效;变体 2 还可以证明编译器选择了正确的模板重载。但是 lambda 是愚蠢的——它所做的只是将单个参数转发到 boost::to_lower_copy。但是变体 3,直接使用函数模板不会编译,即使我实例化它也是如此。我错过了什么?

我有 clang 版本 3.3 (tags/RELEASE_33/rc3),使用 libstdc++-4.8.1-1.fc19.i686 和 boost-1.53​​.0-14.fc19.i686。

vector<string> strings = {"Foo", "Bar"};
vector<string> lower_cased_strings;
transform(
    strings.begin(),
    strings.end(),
    inserter(lower_cased_strings, lower_cased_strings.end()),
//  Variant 1
//  [](const string &word) {
//      return boost::to_lower_copy(word);
//  }
//  Variant 2
//  [](const string &word) {
//      return boost::to_lower_copy<string>(word);
//  }
//  Variant 3
    boost::to_lower_copy<string>
    );

> clang++ -std=c++11 lowercase.cxx

In file included from lowercase.cxx:3:
In file included from /usr/include/boost/algorithm/string.hpp:18:
In file included from /usr/include/boost/algorithm/string/std_containers_traits.hpp:23:
In file included from /usr/include/boost/algorithm/string/std/slist_traits.hpp:16:
In file included from /usr/lib/gcc/i686-redhat-linux/4.8.1/../../../../include/c++/4.8.1/ext/slist:47:
In file included from /usr/lib/gcc/i686-redhat-linux/4.8.1/../../../../include/c++/4.8.1/algorithm:62:
/usr/lib/gcc/i686-redhat-linux/4.8.1/../../../../include/c++/4.8.1/bits/stl_algo.h:4949:33: error: too few arguments to function call, expected 2, have 1
        *__result = __unary_op(*__first);
                ~~~~~~~~~~         ^
lowercase.cxx:11:5: note: in instantiation of function template specialization 'std::transform<__gnu_cxx::__normal_iterator<std::basic_string<char> *, std::vector<std::basic_string<char>,
  std::allocator<std::basic_string<char> > > >, std::insert_iterator<std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > > >, std::basic_string<char> (*)(const std::basic_string<char>
  &, const std::locale &)>' requested here
transform(
^
4

1 回答 1

6

默认函数参数。

boost::to_lower_copy有 3 个参数和 2 个参数版本。您正在实例化的 2 参数版本具有默认参数。所以直接调用的时候不需要提供,是隐式提供的。

然而,函数指针to_lower_copy并没有封装存在默认参数的事实。

于 2013-10-02T21:02:19.403 回答